0

I've just started using Arduino and I'm having trouble getting my code right.

Here is the task I need to do.

"Create a button read function Hint: the function header should be int checkbutton ( void ) in the loop function, add code such that if the button has been pressed (i.e. the checkbutton function returns 1), a message is sent to the serial monitor with your username."

There are no errors in the code but nothing happens in the serial monitor when I press the LED button and it does not return 0 when i don't press the LED button. Any sort of help would be appreciated!

Thanks!

int Button = 15; //LED and output are on the same pin

int last = 0;  

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

int button() {
  pinMode(Button, INPUT); 

  int current = digitalRead(Button); 

  if (current and not last) {       
    delay(5);

    pinMode(Button, OUTPUT);

    Serial.print("Hey");
    return 1;
  }

  else {
    pinMode(Button, OUTPUT);

    current == last;

    return 0;
  }
}

void loop() {
  if (button >=0)
  {
    button();
  }

  else
  {
    ;
  }
} 
  • Look up the StateChangeDetection example that is provided within the Arduino IDE, as well as arduino.cc/en/Tutorial/Button – MichaelT Mar 7 at 3:55
  • Also, it depends on the microcontroller, but on most arduino products the LED is on pin 13 not 15 – MichaelT Mar 7 at 3:56
  • is this a school assignment? – jsotola Mar 7 at 5:18
1

In your button() function, the line

if (current and not last) {

is less common syntax. The more common equivalent is:

if (current && !last) {

(see https://stackoverflow.com/questions/2376448/the-written-versions-of-the-logical-operators)

This line:

current == last;

Has no effect and is incorrect. This ( “==“ ) is not an assignment but a comparison and does not do what you want it to do.

I also think you have this the wrong way around. You want to save the “last” button state to compare it with the current button state when you call button() the next time. So you would need to set “last” to the current button state, like this:

last = current;

The next time you call button(), “last” contains the previous button state, so your comparison to the current button state will work.

And I think your button detection code would work better if you set last = current each time you call button(), whether a button has been pressed or not. Otherwise your code would repeatedly keep printing “Hey” until you stop pushing the button (unless that’s what you want). Like so:

int button() {
  pinMode(Button, INPUT); 

  int current = digitalRead(Button); 

  if (current &&  !last) {       
    ...
    ...

    Serial.print("Hey");
    last = current; // Insert here
    return 1;
  }

  else {
    pinMode(Button, OUTPUT);
    ...

(This has not been tested but should give you a general idea).

| improve this answer | |
  • Thank you very much! Fixed my code and helped increase my understanding, much appreciated. – Zèro Mar 9 at 5:56
0

Your problem may only be that you are not calling the button function. Try changing if (button >=0) to if (button() >=0).

| improve this answer | |
  • Ah yes, you're right. I made the change and now the serial monitor is printing hey but its not returning 1, nor am i getting 0 when i do not press the button. Btw, thanks for the edit! Much appreciated. – Zèro Mar 7 at 3:02
  • If you want to print 0 and 1 to the serial console you'll need to add the Serial.print(...) calls. If I understand what you want correctly, you may be able to replace the entire body of loop() with Serial.println(loop()); – kentavv Mar 7 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.