Quick Quesiton about programming function

I just started with Arduino,and am using a program that make an LED turn on and stay on until I push a Switch.The code is:

int ledpin = 13; int switchPin = 8; boolean lastbutton = low; boolean ledon = false;

VoidSetup() { pinmode(ledpin, output); pinmode(switchpin, input); } voidloop() { if digitalread(switchpin) == HIGH && lastbutton == LOW); { ledon = !ledon; lastbutton = HIGH; } else { lastbutton=digitalread(switchpin); } digitalwrite(ledpin, ledon); } So what I get confused on is the if statement. Heres how I see it: If the digitalread comes up HIGH and the Lastbutton was LOW the LedOn variable turns the LED HIGH/TRUE.Now does this permently set the Lastbutton=HIGH,so when I push the switch again and it starts the void loop again, it automatically sets the lastbutton to HIGH so it goes to the else statement,or does it do a digitalread(switchpin) and says that it is currently low,so it goes to the else statement.And now sets the lastbutton to equal LOW b/c of the lastbutton=digitalread(switchpin) in the else statement and notices it is low b/c I pushed theswitch.

You need to post your actual code. What you have there will not compile. In code tags, please. Copied from the IDE, and pasted into code tags.

ok,I rewrote it so you should be able to copy and paste,and it'll work.

if not,ill have to wait till later to get to my computer.At school now.Probably acouple hours and i’ll beable to post the word for word .

geryu: apart from missing braces {} the code seems to do something:

it starts with unpressed button (last=low) and unlit led.

the loop runs fast again and again, but until you press the switch, it does nothing special, apart from reading the button and confirming led status.

if you press button on start of the loop, it is detected, lastbutton set high and lastled change (to true). Immideatly the loops run again, while you are still keeping the button down (in term of microseconds), this time going by the else clause (lastbutton is high) so setting led to new value (light it). and again and again ...

then you unpress the button and it runs the else clause because digitalread is low. and again and again

until you press the switch to unlit the led.

But there is a catch - if you happen to press the switch after the if but before the else clause reads it, then you go next loop to else clause again, as the lastbutton is already high when you hit the digitalread in if. So it depends on luck, if it works or not. Roulette.

you should do it rather this way:

int nowbutton;
void loop() {
  nowbutton=digitalRead(pinswitch);
  if ((nowbutton==high )  &&  (lastbutton==low)){
     ledon=!ledon;
    digitalWrite(ledpin,ledon);
  }
  lastbutton=nowbutton;
}

so according to my example, digitalwrite(ledpin, ledon); is what either turns the LED on or off depending on what value the variable ledon has at the time of the if statement.Either True(high) or False(low).Correct.I thought Boolean statements ment either true/false, high/low, on/off and basically all ment the same thing of either high/low, on/off,etc.....So they can be interchanged.By this I mean,instead of saying HIGH we can say TRUE.Or am I mistaken.

Or is just !ledon means turn LED HIGH and ledon means turn LED LOW,b/c I thought this was only done with the digitalwrite statement?

I'll give yours a try as well :) All of this stuff interests me

Those should be true/false or HIGH/LOW (note case)

on/off would need to be defined as a 1/0, or 0/1, depending on which value you want to incicate each state,

But yes, a !ledon would toggle the value of the variable ledon from true to false, or vice versa.

There is an interessting thing though. A bool and a boolean are not quite the same thing. A bool can ONLY have the values 0 or 1, while a boolean may have any value from 0 to 255 (it’s an unsigned 8-bit variable). In a boolean, a zero is false, and a non-zero is true.

Here’s a little sketch to illustrate this. Note the !b and the !bn statement, and their effect on the variables.

bool b;
boolean bn;

void setup() {
  Serial.begin(115200);     // opens serial port, sets data rate to 9600 bps

  b = 5;
  bn = 5;
  Serial.println(b);
  b = !b;
  Serial.println(b);

  Serial.println(bn);
  bn = !bn;
  Serial.println(bn);
  Serial.println("-----------");
  
  for (int i = -2; i < 3; i += 2) {
    b = i;
    bn = i;
    Serial.print(b);
    if (b) {
      Serial.println("  b is true");
    }
    else {
      Serial.println("  b is false");
    }
    Serial.print(bn);
    if (bn) {
      Serial.println("  bn is true");
    }
    else {
      Serial.println("  bn is false");
    }
  }
}

void loop() {
}