// RC Transmitter Sample V1.00 // Copyright LABOAR N.Koike #include "TimerOne.h" #define LED 13 #define VIN 0 #define SW1 19 #define SW2 9 #define SW3 10 #define SW4 11 #define RSV 1 #define RSH 2 #define LSV 3 #define LSH 4 #define PPM 12 #define PWS 400 #define PWSA 409 #define PWL 4600 #define PWI 1500 #define MAXCH 8 int rsv,rsh; int lsv,lsh; int sw1,sw2,sw3,sw4; int sw1s,sw2s,sw3s,sw4s; int tc1; int vinv; int CHN; int ch, cht; volatile int chnum = 0; volatile int chppm[MAXCH+1]; volatile int state = LOW; volatile int ledcn = 0; boolean joypad = false; void setup() { pinMode(LED, OUTPUT); pinMode(SW1, INPUT); sw1s = LOW; pinMode(SW2, INPUT); sw2s = LOW; pinMode(SW3, INPUT); sw3s = LOW; pinMode(SW4, INPUT); sw4s = LOW; digitalWrite(LED, HIGH); for(tc1 = 0; tc1 < MAXCH; tc1++) { chppm[tc1] = PWI; } chppm[MAXCH] = PWL + PWS; chnum = 0; pinMode(PPM, OUTPUT); digitalWrite(PPM, state); Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period Timer1.attachInterrupt(makeppm); // attaches makeppm() as a timer overflow interrupt lsv = analogRead(LSV); if (lsv > 400 && lsv < 600) joypad = true; if (!joypad) { Serial.begin(38400); Serial.println("RC Transmitter V1.00 Ready"); while (1) { if (Serial.available()>3){ ch = Serial.read(); if (ch == 67) { //"C" ch = Serial.read(); if (ch == 72) { //"H" CHN = Serial.read() -48; ch = Serial.read() -48; CHN = CHN * 10 + ch; // lcd.println(CHN); // for debug if (CHN >0 && CHN <= MAXCH){ break; } else { // lcd.println("Channel No. ERROR"); delay(800); } } } } } // while } // if (!joypad) } void makeppm() { state = !state; digitalWrite(PPM, state); if (state == LOW){ Timer1.setPeriod(chppm[chnum++]-PWS); if (chnum > MAXCH) chnum = 0; } else { Timer1.setPeriod(PWS); } ledcn += 1; } void loop() { VINcheck(); if (joypad) { rsv = analogRead(RSV); sw1 = digitalRead(SW1); rsh = analogRead(RSH); sw2 = digitalRead(SW2); lsv = analogRead(LSV); sw3 = digitalRead(SW3); lsh = analogRead(LSH); sw4 = digitalRead(SW4); if(sw1 == LOW) { sw1s = !sw1s; } if(sw2 == LOW) { sw2s = !sw2s; } if(sw3 == LOW) { sw3s = !sw3s; } if(sw4 == LOW) { sw4s = !sw4s; } chppm[0] = rsv + 988; chppm[1] = rsh + 988; chppm[2] = lsv + 988; chppm[3] = lsh + 988; chppm[4] = sw1s * 1023 + 988; chppm[5] = sw2s * 1023 + 988; chppm[6] = sw3s * 1023 + 988; chppm[7] = sw4s * 1023 + 988; } // if (joypad) else { while (Serial.available() < 4) { delayMicroseconds(260); } ch = Serial.read(); if (ch == 67) { //"C" ch = Serial.read(); if (ch == 72) { //"H" ch = Serial.read(); ch = Serial.read(); for (tc1=0 ; tc1 < CHN ; tc1++) { while (Serial.available() < 4){ delayMicroseconds(260); } ch = Serial.read(); cht = 1000 * (ch - 48); ch = Serial.read(); cht = cht + 100 * (ch - 48); ch = Serial.read(); cht = cht + 10 * (ch - 48); ch = Serial.read(); cht = cht + (ch - 48); if (cht > 600 && cht < 3000) { chppm[tc1] = cht; } } } } } } void VINcheck() { // VIN Battery check vinv = analogRead(VIN); if(ledcn < 600) { if (vinv < 736) { // < 7.2V 1023 * 7.2 /10 = 736 digitalWrite(LED, HIGH); } else { digitalWrite(LED, LOW); } } else { if (vinv < 757) { // < 7.4V 1023 * 7.4 /10 = 757 digitalWrite(LED, HIGH); } else { digitalWrite(LED, LOW); } } if (ledcn > 1200) ledcn = 0; // end of VIN check }