Displaying when Button has been pressed for LED and returning 1 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 &gt;=0)
  {
    button();
  }

  else
  {
    ;
  }
}
In your button() function, the line

if (current and not last) {


is less common syntax. The more common equivalent is:

if (current &amp;&amp; !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 &amp;&amp;  !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).