Currently writing code that begins with an if statement in the loop function that is meant to add to a count every time the button is pressed so that each time the button is pressed, the LED lights behave differently. The if statement basically is meant to add to the integer "count" then a delay
added for a second to allow the button to close again before checking if the button is pressed. to check this is working, I'm printing the value of count every time it changes.
However, this delay is not working how I intend it to. the button is adding to the count multiple times every time I press the button. Is there something missing in this if statement? every example I've seen is the same as how I've done it.
The screenshot of the circuit diagram below, the code below too. I will add that there is an external input signal in pin 3 (normally 2 but circuito.io would let me swap the pins)
The part I'm having issue with:
const byte buttonPin = 4;
byte count = 0; // counts the amount of times the button has been pressed
void setup() {
digitalWrite(buttonPin, HIGH);
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
count++; //increase the counter once the button is released
Serial.println(count);
}
}
The entire code:
const byte redLEDPin = 5; // set red led output to 5 (PWM pin, not sure if that is goingto beb necessary since its a logical output )
const byte greenLEDPin = 6; // set green led to 6 (PWM pin)
const byte buttonPin = 2;
byte count = 0; // counts the amount of times the button has been pressed
volatile int redLEDState; // initial state of led, let us know if the circuit is on
volatile int greenLEDState;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(2, INPUT);
redLEDState = HIGH;
greenLEDState = LOW;
digitalWrite(redLEDPin, redLEDState);
digitalWrite(greenLEDPin, greenLEDState);
digitalWrite(buttonPin, HIGH);
attachInterrupt(digitalPinToInterrupt(3), PulseISR, CHANGE);
noInterrupts();
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
count++; //increase the counter once the button is released
Serial.println(count);
}
// MODES
// MODE 1: INITAL PHASE
if (count == 0) {
noInterrupts();
redLEDState = 1;
greenLEDState = 1;
digitalWrite(redLEDPin, redLEDState);
digitalWrite(greenLEDPin, greenLEDState);
delay(1000);
redLEDState = 0;
greenLEDState = 0;
digitalWrite(redLEDPin, redLEDState);
digitalWrite(greenLEDPin, greenLEDState);
delay(1000);
}
// MODE 2: constant red mode
if (count == 1) {
redLEDState = HIGH;
greenLEDState = LOW;
digitalWrite(redLEDPin,redLEDState);
digitalWrite(greenLEDPin,greenLEDState);
}
// MODE 3: constant green mode
if (count == 2) {
redLEDState = LOW;
greenLEDState = HIGH;
digitalWrite(redLEDPin,redLEDState);
digitalWrite(greenLEDPin,greenLEDState);
}
// MODE 4: aLternating LEDS mode
if (count == 3) {
redLEDState = HIGH;
greenLEDState = LOW;
digitalWrite(redLEDPin,redLEDState);
digitalWrite(greenLEDPin,greenLEDState);
interrupts();
}
// RESET
if (count == 4) {
count = 0;
}
}
void PulseISR() {
if (redLEDState == HIGH) {
redLEDState = LOW;
greenLEDState = HIGH;
digitalWrite(redLEDPin, redLEDState);
digitalWrite(greenLEDPin, greenLEDState);
}
else if (greenLEDState == HIGH) {
redLEDState = HIGH;
greenLEDState = LOW;
digitalWrite(redLEDPin, redLEDState);
digitalWrite(greenLEDPin, greenLEDState);
}
else {
Serial.print("error");
}
}