I wasn't able to work on this project this mMonday since my project was locked in the building. Right now, it isn't functioning the way I want it to. I seem to be having trouble with embedding loops and debouncing simultaneously.
int inPin1 = 2; // the number of the input pins
int inPin2 = 4;
int inPin3 = 7;
int outPin1 = 11; // the number of the output pins
int outPin2 = 10;
int outPin3 = 9;
int outPin4 = 6;
int outPin5 = 13;
int outPin6 = 5;
int outPin7 = 3;
int outPin8 = 8;
int outPin9 = 12;
int state1 = LOW; // the current states of the output pins
int state2 = LOW;
int state3 = LOW;
int reading1; // the current readings from the input pins
int reading2;
int reading3;
int previous1 = LOW; // the previous readings from the input pins
int previous2 = LOW;
int previous3 = LOW;
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
int value = 0;
void setup()
{
pinMode(inPin1, INPUT);
pinMode(inPin2, INPUT);
pinMode(inPin3, INPUT);
pinMode(outPin1, OUTPUT);
pinMode(outPin2, OUTPUT);
pinMode(outPin3, OUTPUT);
pinMode(outPin4, OUTPUT);
pinMode(outPin5, OUTPUT);
pinMode(outPin6, OUTPUT);
pinMode(outPin7, OUTPUT);
pinMode(outPin8, OUTPUT);
pinMode(outPin9, OUTPUT);
}
void loop()
{
//first button/LED
reading1 = digitalRead(inPin1);
if (reading1 == HIGH && previous1 == LOW && millis() - time > debounce) {
// ... invert the output
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;
time = millis();
}
digitalWrite(outPin1, state1);
previous1 = reading1;
/*
if (state1 == HIGH) {
digitalWrite(outPin1, state1);
digitalWrite(outPin8, state1);
digitalWrite(outPin9, state1);
previous1 = reading1;
}
*/
/*
//failed attempt at PWM
if (state1 == HIGH) {
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
analogWrite(inPin1, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(inPin1, value);
delay(30);
}
}*/
//second button/LED
reading2 = digitalRead(inPin2);
if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce) {
// ... invert the output
if (state2 == HIGH)
state2 = LOW;
else
state2 = HIGH;
time = millis();
}
digitalWrite(outPin2, state2);
previous2 = reading2;
//third button/LED
reading3 = digitalRead(inPin3);
if (reading3 == HIGH && previous3 == LOW && millis() - time > debounce) {
// ... invert the output
if (state3 == HIGH)
state3 = LOW;
else
state3 = HIGH;
time = millis();
}
digitalWrite(outPin3, state3);
previous3 = reading3;
}