Hi,
did any of you tried to run more than two switches from the tutorial:
https://www.arduino.cc/en/tutorial/switch
I tried three and last one is blinking only:)
regards
Hi,
did any of you tried to run more than two switches from the tutorial:
https://www.arduino.cc/en/tutorial/switch
I tried three and last one is blinking only:)
regards
(deleted)
I tried three and last one is blinking only:)
I can see no problem with the code that you have not posted
here is the code:
int in0 = 0; // the number of the input pin
int in1 = 1; // the number of the input pin
int in2 = 2; // the number of the input pin
int Out10 = 10; // the number of the output pin
int Out11 = 11; // the number of the output pin
int Out12 = 12 ; // the number of the output pin
int state0 = LOW; // the current state of the output pin
int reading0; // the current reading from the input pin
int previous0 = HIGH; // the previous reading from the input pin
int state1 = LOW; // the current state of the output pin
int reading1; // the current reading from the input pin
int previous1 = HIGH; // the previous reading from the input pin
int state2 = LOW; // the current state of the output pin
int reading2; // the current reading from the input pin
int previous2 = HIGH; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time0 = 0; // the last time the output pin was toggled
long time1 = 0; // the last time the output pin was toggled
long time2 = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(in0, INPUT);
pinMode(in1, INPUT);
pinMode(in2, INPUT);
pinMode(Out10, OUTPUT);
pinMode(Out11, OUTPUT);
pinMode(Out12, OUTPUT);
}
void loop()
{
//switch reading
reading0 = digitalRead(in0);
reading1 = digitalRead(in1);
reading2 = digitalRead(in2);
//first switch section
{
if (reading0 == HIGH && previous0 == LOW && millis() - time0 > debounce) {
if (state0 == HIGH)
state0 = LOW;
else
state0 = HIGH;
time0 = millis();
}
previous0 = reading0;
}
{
//second switch section
if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce) {
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;
time1 = millis();
}
previous1 = reading1;
}
{
//third switch section
if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce) {
if (state2 == HIGH)
state2 = LOW;
else
state2 = HIGH;
time2 = millis();
}
previous2 = reading2;
}
digitalWrite(Out10, state0);
digitalWrite(Out11, state1);
digitalWrite(Out12, state2);
}
result is that switch#1 and #2 is working fine and last one is blinking. Blinking frequency is equal to the debounce time.
I have tested that in different configurations, inputs, outputs and results is every time the same. Third section/output is blinking....
pls advice
Comments :
It is not a good idea to use pins 0 and 1 as digital inputs as they are normally used by the Serial interface
How are the inputs wired ? Have you got pulldown resistors in place to keep them in a known state ?
Timing variables associated with millis() should be unsigned longs, not just longs
Many of your variables are declared as int when they could just as well be byte
Pin number declarations could be const as they will never change
There are unnecessary pairs of braces in your code
@UKHeliBob,
code is taken from this web (https://www.arduino.cc/en/tutorial/switch). I multiply it to to enable three section which I need for my project.
I rechecked the switch connection and there was no HIGH signal. After adding resistor (from 5V to switch) problem is solved.
Thx for suggestions.
I am glad you got it working, but note my comments which are generally applicable. In particular I suggest that you stay clear of pins 0 and 1 as you will need to use the Serial interface at some time. A further suggestion. Look at how pinMode(pinNumber, INPUT_PULLUP) works and eliminates the need for external resistors to keep inputs in a known state instead of floating at an uncertain voltage.
Now you have it working consider improving it by using arrays to hold the values for each switch and LED which will avoid the need to repeat the code 3 times. When you get that working, which will be a good exercise, try using an array of structs which will tidy things up even further.
Agreed, that code is in desperate need of arrays (as a rule of thumb: the moment you start using numbers in your variable names, you probably need arrays).
Also don't ever call pins "input1" or so. Give them a descriptive name, telling you what they do (which is important) rather than where they're connected to (which is not important any more after you set the number in your code).