OK I have a short piece of code I wrote and not working like it should. or the way I think it should work and I cannot figure out what I did wrong. It compiles, just does not do much of anything except Serial Print two integers that never change value.
The code is pretty simple. I want to use Digital Pins 20, 21, and 22 as Inputs with Pullup to set them as High or 1. I am using 2 switches. One on/off switch on Pin 20 to set var AI_GEAR to either 800 or 2200. The second switch is a SPDT-Center Off, and is used to change var AI_AUX to 800, 1500, or 2200. Both switches have Ground connected to the common contact to togle the digital pins low or 0. Then all the program does is Print the vars AI_GEAR and AI_AUX all day long. Unfortunately all it does is Print:
AI_GEAR = 800
AI_AUX = 2200
All day long. The values do not change as I ground pins 20, 21, and 22. Where did I go wrong?
The code is pretty simple. I want to use Digital Pins 20, 21, and 22 as Inputs with Pullup to set them as High or 1. I am using 2 switches. One on/off switch on Pin 20 to set var AI_GEAR to either 800 or 2200. The second switch is a SPDT-Center Off, and is used to change var AI_AUX to 800, 1500, or 2200. Both switches have Ground connected to the common contact to togle the digital pins low or 0. Then all the program does is Print the vars AI_GEAR and AI_AUX all day long. Unfortunately all it does is Print:
AI_GEAR = 800
AI_AUX = 2200
All day long. The values do not change as I ground pins 20, 21, and 22. Where did I go wrong?
Code:
// Initialize digital pins 20, 21, and 22 as integers
int D1_GEAR = 20; //
int D1_AUX = 21; //
int D2_AUX = 22; //
// Initialize vars
int AI_GEAR = 800;
int AI_AUX = 800;
void setup() {
// set digital pins 20, 21, and 22 as Inputs and Pullup.
pinMode(D1_GEAR, INPUT_PULLUP);
pinMode(D1_AUX, INPUT_PULLUP);
pinMode(D2_AUX, INPUT_PULLUP);
Serial.begin(9600); // initialize serial communications:
}
void loop() {
//Set Gear channel to 800 or 2200
if (D1_GEAR == 0)
{AI_GEAR = 800;}
else {AI_GEAR = 2200;}
//Set Aux channel to either 800, 1500, or 2200
if (D1_AUX == LOW && D2_AUX == LOW)
{AI_AUX = 1500;}
else if (D1_AUX == LOW && D2_AUX == HIGH)
{AI_AUX = 800;}
else if (D1_AUX == HIGH && D2_AUX == LOW)
{AI_AUX = 2200;}
else if (D1_AUX == HIGH && D2_AUX == HIGH)
{AI_AUX = 800;}
//Print Gear and Aux channel values
Serial.print("AI_GEAR =");
Serial.print(AI_GEAR);
Serial.print("AI_AUX =");
Serial.print(AI_AUX);
delay(1000); // delay in between reads for stability
}
Last edited: