// This sketch is for an Arduino NANO. // It connects to a radio controll reciever's servo port, and controls relays based on the stick position. // This version supports in-field programming. char tog=0; #define ledPin 13 // What pin is our LED connected to. #define ServoInputPin 12 // Connect the R/C to this Arduino Nano input pin #define RelayPin1 11 // Connect Relay #1 to this Arduino Nano output pin #define RelayPin2 10 // etc for #2 and so on:- #define RelayPin3 9 #define RelayPin4 8 #define numRelays 4 int relays[numRelays]={RelayPin1,RelayPin2,RelayPin3,RelayPin4}; //here's an example for using 8 relays - comment the above 2 lines with "//" and uncomment below to use. //#define numRelays 8 //int relays[numRelays]={11,10,9,8,7,6,5,4}; #define doserial 0 // For debugging: show the data we're getting on the serial ports as well #define stabledelay 200 // This is the number of milliseconds we must get the same relay reading before we consider that this means "on"... this is to stop interference etc from triggering the relays // We divide the full stick throw into 9 time slots (4 slots activate the relays, and the 5 slots either-side deactivate them) #define pulseMin 1056 #define pulseMax 2028 #define pulseWidth ((pulseMax-pulseMin)/(2*numRelays+1)) void setup() { if(doserial)Serial.begin(115200); // We also output computer debugging information to the serial port pinMode(ledPin, OUTPUT); // onboard LED - we flash this to indicate stuff. pinMode(ServoInputPin, INPUT); // Connect the servo control line (brown) to here // Connect the RC reciever ground (0v - black) to GND as well. pinMode(RelayPin1, OUTPUT); // Connect these to the relay block pinMode(RelayPin2, OUTPUT); pinMode(RelayPin3, OUTPUT); pinMode(RelayPin4, OUTPUT); // The relay block needs GND and +5v connected as well if(doserial)Serial.println("\r\nPat's R/C relay board Version 2.0"); // Blink 3 times to show we are running OK for(int i=0; i<3; i++) {digitalWrite(ledPin,1); delay(500); digitalWrite(ledPin,0); delay(500);} } int servopulse=0; int lastpulse=0; unsigned long microStart,microEnd,microTime; int currentRelay=0; int lastRelay=0; void loop() { servopulse=digitalRead(ServoInputPin); if(servopulse!=lastpulse) { // The start or end of a signal lastpulse=servopulse; if(servopulse) { // The start of a signal microStart=micros(); } else { // The end of a signal microEnd=micros(); microTime=0; if(microEnd>microStart)microTime=microEnd-microStart; // skip the 9hour rollover currentRelay=WhichRelay(microTime); // N.B. returns 0 for no relay at all if((DebounceRelay(currentRelay))&&(currentRelay!=lastRelay)) { // has something change (for long enough) lastRelay=currentRelay; // Remmeber what we're doing SetRelay(currentRelay); // Energize (or de-energize if 0) the relay now. if(lastRelay>0)digitalWrite(ledPin,1); else digitalWrite(ledPin,0); // turn our LED on anytime we energize a relay } LedInfo(); if(doserial)Serial.print(currentRelay); if(doserial)Serial.print("\t"); if(doserial)Serial.print(lastRelay); if(doserial)Serial.print("\t"); if(doserial)Serial.print(microTime); if(doserial)Serial.print("\t"); if(doserial)Serial.println(""); } } } // loop // *****************Relay1************************ *******************Relay2********************** ******************Relay3*********************** *******************Relay4********************** // -1- -1- -1- -1- -1- -1- -1- -1- -1- -1- -1- -1- -2- -2- -2- -2- -2- -2- -2- -2- -2- -2- -2- -2- -3- -3- -3- -3- -3- -3- -3- -3- -3- -3- -3- -3- -4- -4- -4- -4- -4- -4- -4- -4- -4- -4- -4- -4- -5- -5- -5- -5- -5- -5- -5- -5- -5- -5- -5- -5- -6- -6- -6- -6- -6- -6- -6- -6- -6- -6- -6- -6- -7- -7- -7- -7- -7- -7- -7- -7- -7- -7- -7- -7- -8- -8- -8- -8- -8- -8- -8- -8- -8- -8- -8- -8- -9- -9- -9- -9- -9- -9- -9- -9- -9- -9- -9- -9- -10 // 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 // Figure out which relay is wanted from this pulse length int WhichRelay(unsigned long pulselength) { for(int i=0; i=myPulse)&&(pulselength<(myPulse+pulseWidth)))return (1+i); } return 0; } // WhichRelay // Energize the chosen relay int SetRelay(int whichrelay) { int pinval; if(whichrelay==1)pinval=1; else pinval=0; digitalWrite(RelayPin1, pinval); if(whichrelay==2)pinval=1; else pinval=0; digitalWrite(RelayPin2, pinval); if(whichrelay==3)pinval=1; else pinval=0; digitalWrite(RelayPin3, pinval); if(whichrelay==4)pinval=1; else pinval=0; digitalWrite(RelayPin4, pinval); return whichrelay; } // SetRelay int lastDebounce=0; unsigned long start, finished, elapsed; // We never select or de-select any relay unless we have had at leas 200ms (or stabledelay) stable signal for it int DebounceRelay(int whichrelay) { if(whichrelay!=lastDebounce) { // something has changed lastDebounce=whichrelay; // remember it start=millis(); // restart the timer } else { // Same relay as last time finished=millis(); elapsed=finished-start; // How much time has gone by if(elapsed>=stabledelay) { // Enough! time to change it! return 1; // signal caller that enough time has gone by now! } } return 0; // Not enough time has yet passed. } // DebounceRelay void LedInfo() { //if(tog++&1)digitalWrite(ledPin,0); else digitalWrite(ledPin,1); }