Digital output to drive LED

In the code below, my logic does not change the value of the boolean variable 'recordingstatus'.
The pushbutton wired to PIN4 works fine, changing from 0-5 V when pressed. The associated boolean variable 'buttonstate' changes as expected when the button is pressed.
Any assistance will be much appreciated!

#include <SPI.h>
//#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <SeeedOLED.h>

#define redLEDpin 2              // digital 2
#define greenLEDpin 3            // digital 3
#define pushbuttonpin 4          // digital 4
#define tempPCBpin 1             // analog 1

bool recordingstatus;
bool buttonstate;

void setup(void)
{
Serial.begin(9600);

pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,INPUT);

  recordingstatus=0;            // set recording status to OFF

}

void loop()
{
  buttonstate=digitalRead(pushbuttonpin);
  if ((buttonstate==true) && (recordingstatus==false)){recordingstatus=true;}
  if ((buttonstate==true) && (recordingstatus==true)){recordingstatus=false;}
 
  // red LED indicates not recording
  if (recordingstatus==false){digitalWrite(redLEDpin,HIGH);}
  //if (recordingstatus==false){digitalWrite(greenLEDpin,LOW);}
  //if (recordingstatus==true){digitalWrite(greenLEDpin,HIGH);}
  if (recordingstatus==true){digitalWrite(redLEDpin,LOW);}
  delay(1000);


Serial.print(buttonstate);
Serial.print(recordingstatus);

  analogRead(tempPCBpin); 
  delay(10);
  int tempReading = analogRead(tempPCBpin);    
  
}


else if ((buttonstate==true) && (recordingstatus==true)){recordingstatus=false;}

1 Like
   buttonstate = digitalRead(pushbuttonpin);
   if( (buttonstate == true) && (recordingstatus == false) ) {
      recordingstatus = true;
   }
   if( (buttonstate == true) && (recordingstatus == true) ) {
      recordingstatus = false;
   }

Assume buttonState is true/HIGH/1.

If recordingstatus is false, the first if statement sets recordingstatus to true.

Since recordingstatus is now true, the second if statement sets it back to false.

1 Like

Hi guys
Amazingly quick response - thank you!
Its my first day of Arduino coding. I now know not to spend 3 hours trying to debug syntax and logic errors when there are willing helpers like you out there.
I revised the code (as below) and it works!
Cheers for now....

if ((buttonstate==true) && (recordingstatus==true)){recordingstatus=false;}
 else if ((buttonstate==true) && (recordingstatus==false)){recordingstatus=true;}
if ((buttonstate==true) && (recordingstatus==true))
{
    recordingstatus=false;
}
else if ((buttonstate==true) && (recordingstatus==false))
{
    recordingstatus=true;
}
  • For readability, highly recommend you format your code as seen above.
1 Like

But you have mixed up data types and its vaues.

1.
The digitalRead() function returns either HIGH or LOW (Fig-1) to store which int type variable is used. So,


Figure-1:

2.
bool buttonstate = digitalRead(pushbuttonpin);

==>
int buttonstate = digitalRead(pushbuttonpin);

3.

if ((buttonstate==true) && (recordingstatus==false))
{
   recordingstatus=true;
}

==>

if ((buttonstate == HIGH) && (recordingstatus == false))
{
    recordingstatus = true;
}
1 Like

It's simpler to me this way

  if (buttonstate == true) {
    if (recordingstatus == false) {
      recordingstatus = true;
    }
    else {
      recordingstatus = false;
    }
  }

Do something if the button is down.

But they are boolean variabkes, so

  if (buttonstate) {
    recordingstatus = !recordingstatus;
  }

You can use not instead of !

  if (buttonstate) {
    recordingstatus = not recordingstatus;
  }

If you ever shorten

  delay(1000);

you will easily notice that keeping your finger on the button too long will result in switching back and forth.

You probably want to toggle recordingstatus when the button goes down, not if it is still down.

You will sooner later want to know how to do that.bThere is an example in the IDE that goes with this article that gets into it:

https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection/

HTH

edit to edit onaconna

which is actually a peeve of mine.

a7

My advice is that, for best code readability, bool variables should be given names that reflect what true means.

bool recording;
bool buttonPressed;

for example.

And rather than

if ((buttonstate==true) && (recordingstatus==false)){recordingstatus=true;}

Write more readable code like

if (buttonPressed and not recording) {
  recording=true;
}

because and, or and not are more readable and it is unnecessary to compare a bool variable with true. Also remove unnecessary brackets, which also reduce readability.

1 Like