Newbie programming question

Hello,

I am trying to set a while loop in motion when a switch is pressed high, mainly the digital pin 2.

The code I have is below. I know the while loop works fine, but I am not sure about setting up the digital section to work when the switch is high. I have tried the best I could from the reference manual, but no joy. Any advice would be great.

An error I get is “‘D2’ was not declared in this scope”.

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

int var = 0;

void loop()
{
  int buttonValue = digitalRead(D2);
  if(buttonValue, HIGH)
  {
    while (var < 20)
      {
        Serial.println("Hello");
        var++;
        delay (2000);
      }
  }
}

Thanks in advance.

Seán

Try:

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

int var = 0;

void loop()
{
  int buttonValue = digitalRead(2); // digital pins are just numbers, no leading D
  if(buttonValue == HIGH)             // no comma, just == as a test for equality
  {
    while (var < 20)
      {
        Serial.println("Hello");
        var++;
        delay (2000);
      }
  }
}

Ah, yes, that did the trick.

Thanks a lot.

Seán