Did a Nano project last year to read 6 switch inputs and generate servo outputs accordingly. It works perfectly - code at bottom.
Recently I started another Nona project , this time to control model railroad semaphores (using servos) according to inputs from track status lines. I had "Nano pin numbers.jpg" in my reference file (copy attached) and I was going friggin crazy trying to get my inputs to work!
I referred back to the previous project and realized the pin numbers in software didn't match the pin numbers in the attachment! I spent a day trying to figure it out until I ran into a different reference (attached "nano.pdf") and realized that the "software pin numbers" don't match the physical pin numbers of the board.
I hopetohell I have it right now, before I go back to t he new project and revise the software!!!
(Seems silly to have software and hardware pin number be different!)
#include <Servo.h>
const int switchPin1 = 14; // A0 active low
const int switchPin2 = 15; // A1 active low
const int switchPin3 = 16; // A2 active low
const int switchPin4 = 17; // A3 active low
const int switchPin5 = 18; // A4 active low
const int switchPin6 = 19; // A5 active low
int cw = 10 ;
int ccw = 160 ;
int switch1 = 0 ;
int switch2 = 0 ;
int switch3 = 0 ;
int switch4 = 0 ;
int switch5 = 0 ;
int switch6 = 0 ;
Servo servo1 ;
Servo servo2 ;
Servo servo3 ;
Servo servo4 ;
Servo servo5 ;
Servo servo6 ;
void setup() {
Serial.begin(9600);
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPin2, INPUT_PULLUP);
pinMode(switchPin3, INPUT_PULLUP);
pinMode(switchPin4, INPUT_PULLUP);
pinMode(switchPin5, INPUT_PULLUP);
pinMode(switchPin6, INPUT_PULLUP);
servo1.attach(5) ; // D5
servo2.attach(6) ; // D6
servo3.attach(7) ; // D7
servo4.attach(8) ; // D8
servo5.attach(9) ; // D9
servo6.attach(10) ; // D10
Serial.println ("Running") ;
}
void loop() {
switch1 = digitalRead(switchPin1) ;
Serial.print ("SW 1 ") ;
Serial.println (switch1) ;
if (switch1 == 0)
servo1.write(cw) ;
else
servo1.write(ccw) ;
switch2 = digitalRead(switchPin2) ;
Serial.print ("SW 2 ") ;
Serial.println (switch2) ;
if (switch2 == 0)
servo2.write(cw) ;
else
servo2.write(ccw) ;
switch3 = digitalRead(switchPin3) ;
Serial.print ("SW 3 ") ;
Serial.println (switch3) ;
if (switch3 == 0)
servo3.write(cw) ;
else
servo3.write(ccw) ;
switch4 = digitalRead(switchPin4) ;
Serial.print ("SW 4 ") ;
Serial.println (switch4) ;
if (switch4 == 0)
servo4.write(cw) ;
else
servo4.write(ccw) ;
switch5 = digitalRead(switchPin5) ;
Serial.print ("SW 5 ") ;
Serial.println (switch5) ;
if (switch5 == 0)
servo5.write(cw) ;
else
servo5.write(ccw) ;
switch6 = digitalRead(switchPin6) ;
Serial.print ("SW 6 ") ;
Serial.println (switch6) ;
if (switch6 == 0)
servo6.write(cw) ;
else
servo6.write(ccw) ;
delay(500); //delay for debounce
}
nano.pdf (598 KB)