Hi Everyone,
The Background:
I have two SPDT (ON-OFF-ON) switches attached to my UNO R3, one Momentary switch and one Maintained switch.
The idea is to use these as a kind of throttle and direction control with the Maintained switch being (Fast-Med-Slow) and the Momentary switch being (Up-Stopped-Down).
I have 5V going to both switches.
The Momentary switch is wired to A0
The Maintained switch is wired to A1
(for both switches Pin 1 = 5V, Pin 2 = Analog In, Pin 3 = Ground)
the rest of the Analog pins are empty.
in my mind I should be able to just read the analog pins and have my direction and speed.
A0 = 0, A1 = 0, Slow Up
A0 = 0, A1 = 512, Med Up
A0 = 0, A1 = 1023, FastUp
A0 = 1023, A1 = 0, Slow Down
A0 = 1023, A1 = 512, Med Down
A0 = 1023, A1 = 1023, Fast Down
Anything else = Stopped
(I know this isn't exact and I can expect some fluctuation especially with the med range, but it is a general overview of what I'm aiming for and how I plan to get there)
The Problem:
I put together a quick code to monitor my analog pins so I could get an idea on what is going on and what I could expect as I start my actual coding. Originally I was only looking at the 2 pins i was using but i expanded it to look at all 6 to try and figure out what is going on, even though there is nothing wired to the remaining 4 pins.
the issue is that I am seeing the voltage fluctuate across all the analog pins depending on what state the switches are in.
the largest issue is when the Maintained switch is in the OFF position, A1 can read an almost full 5V or almost completely grounded depending on where my momentary switch is (the momentary switch should only affect A0).
I'm confused if this is something I did (or didn't do) or maybe just an issue with the board. any help is appreciated.
I have attached a txt file of my output as I go through different switch configurations.
Here is the code I used to monitor the analog pins
/*
Analog input monitor
*/
// These constants won't change. They're used to give names to the pins used:
const int analogInPin1 = A0;
const int analogInPin2 = A1;
const int analogInPin3 = A2;
const int analogInPin4 = A3;
const int analogInPin5 = A4;
const int analogInPin6 = A5;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
// print the results to the Serial Monitor:
Serial.print("A0 = ");
Serial.print(analogRead(analogInPin1));
Serial.print("\t A1 = ");
Serial.print(analogRead(analogInPin2));
Serial.print("\t A2 = ");
Serial.print(analogRead(analogInPin3));
Serial.print("\t A3 = ");
Serial.print(analogRead(analogInPin4));
Serial.print("\t A4 = ");
Serial.print(analogRead(analogInPin5));
Serial.print("\t A5 = ");
Serial.print(analogRead(analogInPin6));
if (analogRead(analogInPin1)==0 and analogRead(analogInPin2)==0) {
Serial.println("\t SLOW UP");
}
else if (analogRead(analogInPin1)==1023 and analogRead(analogInPin2)==0) {
Serial.println("\t SLOW DOWN");
}
else if (analogRead(analogInPin1)==0 and analogRead(analogInPin2)==1023) {
Serial.println("\t FAST UP");
}
else if (analogRead(analogInPin1)==1023 and analogRead(analogInPin2)==1023) {
Serial.println("\t FAST DOWN");
}
else {
Serial.println("\t STOPPED");
}
// wait 2 milliseconds before the next loop
delay(2);
}
AnalogOutput.txt (55.1 KB)


