kulifuli-arduino/kulifuli-arduino.ino

92 lines
1.8 KiB
C++

void setup () {
Serial . begin (9600) ;
// 122Hz @ 50% on pin 3 using T/C0
pinMode ( 3 , OUTPUT ) ; // 0C0B is connected to pin 3
TCCR0A =
0 << COM0A1 |
0 << COM0A0 |
1 << COM0B1 |
0 << COM0B0 |
1 << WGM01 |
1 << WGM00 ;
TCCR0B =
1 << WGM02 |
1 << CS02 |
0 << CS01 |
1 << CS00 ;
OCR0A = 127 ;
OCR0B = 63 ;
// 25kHz @ variable duty cycle on pins 9 and 10 using T/C1
pinMode ( 9 , OUTPUT ) ; // 0C1A is connected to pin 9
pinMode ( 10 , OUTPUT ) ; // 0C1B is connected to pin 10
TCCR1A =
1 << COM1A1 |
0 << COM1A0 |
1 << COM1B1 |
0 << COM1B0 |
0 << COM1C1 |
0 << COM1C0 |
1 << WGM11 |
0 << WGM10 ;
TCCR1B =
1 << WGM13 |
1 << WGM12 |
0 << CS12 |
1 << CS11 |
0 << CS10 ;
ICR1H = 0 ;
ICR1L = 79 ;
OCR1AH = 0 ;
OCR1AL = 39 ;
OCR1BH = 0 ;
OCR1BL = 39 ;
}
// Sequence is 188 92 136 14 154 39 154 139 {duty-cycle}
int seqPos = 0 ;
void loop () {
if ( Serial . available () ) {
int input = Serial . read () ;
// In order to avoid serial writes by software other than kulifuli-host messing things up,
// we check for a specific sequence of 8 bytes before the byte that contains the new duty cycle
if (
seqPos == 0 && input == 188 ||
seqPos == 1 && input == 92 ||
seqPos == 2 && input == 136 ||
seqPos == 3 && input == 14 ||
seqPos == 4 && input == 154 ||
seqPos == 5 && input == 39 ||
seqPos == 6 && input == 154 ||
seqPos == 7 && input == 139
) {
// Advance in the sequence
seqPos ++ ;
} else if ( seqPos == 8 ) {
// Sequence completed, `input` is the new duty cycle
OCR1AL = input ;
OCR1BL = input ;
Serial . write (input) ;
seqPos = 0 ;
} else {
// Reset sequence progress
seqPos = 0 ;
}
}
}