Hi, all.
I've got a toggle switch connected to ground and pin 1. An IF statement specifies what the program does when the toggle is in the ON position, while and ELSE statement specifies what to do in the OFF position.
A digitalRead checks the state of pin 1. When the toggle is ON, the program successfully identifies this and enters and executes the IF. My tests all look good in the serial monitor. When the toggle is in the OFF position, though, the program bypasses the IF, but doesn't execute the ELSE, either. Here's my code. Please help!
int secondPin = 10; // second meter connected to DIGITAL PIN 10 -- used for counting, not for display
int minutePin = 3; // minute meter (Ammeter) connected to DIGITAL PIN 3
int hourPin = 9; // hour meter (ammeter) connected to DIGITAL PIN 9
int secondPosition = 0; //inital setting for the ammeter--31 is HALF position, 62 is FULL position
int minutePosition = 0;
int hourPosition = 0;
int setSwitch = 1; //alarm set switch is connected to this pin
int setSwitchState = 1;
void setup() {
// nothing happens in setup
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(setSwitch, INPUT);
}
void loop() {
//increment second meter (secondPin) by seconds, increment minute meter (minutePin) by minutes
int setSwitchState=digitalRead(setSwitch); //check if the alarm mode switch is ON or OFF
//test setSwitch, see if alarm switch is on or off
Serial.print("setSwitchState=");
Serial.print(setSwitchState);
Serial.print('\n');
if (setSwitchState==0) { //if alarm mode is set to OFF, run clock program
Serial.print("Alarm Set Mode is OFF ");
analogWrite(secondPin, secondPosition); //sets second meter to 0 position
analogWrite(minutePin, minutePosition); //sets minute meter to 0 position
analogWrite(hourPin, hourPosition); //sets hour meter to 0 position
delay(1000);
secondPosition=secondPosition+1;
analogWrite(secondPin, secondPosition);
if(secondPosition>60) {
secondPosition=0;
minutePosition=minutePosition+1;
if(minutePosition>60) {
minutePosition=0;
hourPosition=hourPosition+5;
if(hourPosition>60) {
hourPosition=0;
}
}
}
//print time to Serial Monitor
Serial.print(hourPosition);
Serial.print(":");
Serial.print(minutePosition);
Serial.print(":");
Serial.print(secondPosition);
Serial.print('\n');
}
else { // if alarm mode is set to ON, disable clock program
Serial.print("Alarm Set Mode is ON");
Serial.print('\n');
}
}
OR
Maybe it is executing the ELSE statement but then it's unable to use the serial because your toggle switch has tied up the tx line (pin 1 is used for tx on the serial port)
Have you tried using pin 2 for your switch instead?
Thanks for the response!
Just now I tried it on pin 7 instead (pin 2 is being used by something else) by changing my setSwitch variable to 7 and moving the wire on my board. Now it doesn't recognize when I move the toggle switch at all... the pin reads as LOW and keeps cycling through the IF.
dirigible83:
Thanks for the response!
Just now I tried it on pin 7 instead (pin 2 is being used by something else) by changing my setSwitch variable to 7 and moving the wire on my board. Now it doesn't recognize when I move the toggle switch at all... the pin reads as LOW and keeps cycling through the IF.
Another mistake I sometimes make, after updating my code; I'll hit the verify button instead of upload. Then become surprised that my mods have not worked. You could hit the button again, just in case
I had a pull down resistor, and just tried it with a pull up instead. Same issue. I guess I'll have to follow Runaway Pancake's advice and get back to basics. Will keep you posted.
Just in case you're unfamiliar with the jargon, the attached dwg shows an input-switch arrangement with a pull-up resistor.
Point "A" get connected to your chosen input pin -- so long as that's not 0 or 1 (-:
(The "internal_pullup" thing is kind of shaky, to me, but this is minimal, solid.)
I apologize for the long delay between posts. I only infrequently get to work on this project.
So, I wrote a simple sketch to test the switch on PIN 7:
int setSwitch = 7; //alarm set switch is connected to this pin
void setup() {
// nothing happens in setup
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(setSwitch, INPUT);
}
void loop() {
//increment second meter (secondPin) by seconds, increment minute meter (minutePin) by minutes
int setSwitchState=digitalRead(setSwitch); //check if the alarm mode switch is ON or OFF
//test setSwitch, see if alarm switch is on or off
Serial.print("setSwitchState=");
Serial.print(setSwitchState);
Serial.print('\n');
}
When the switch is open, the pin is at state "1". When I close the switch, COM 3 disappears and I can no longer communicate with the board. Ideas? I'm sure something really simple is wrong here.
byte switchPin = 7; //alarm set switch is connected to this pin
byte presentState;
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(switchPin, INPUT);
}
void loop()
{
presentState = digitalRead(switchPin);
if (presentState == 1)
{
Serial.println("Open");
}
else // it's not HI, so it's LO
{
Serial.println("Closed");
}
delay(50); // change for more mania
}
Maybe try using a piece of wire to simulate the switch, at first.
Holy smokes! I was looking at the breadboard sideways, as though the connections ran the other way. I've used breadboards before, but this is my first one that doesn't explicitly have (+) and (-) marked strips running down each side.
Thank you SO MUCH. It's working now.