Friday, May 27, 2022

tinkercat hardware interrupt


when button is pushed, red led turns on right away

volatile byte buttonState = LOW;

void setup()
{
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(2, INPUT);
  attachInterrupt(digitalPinToInterrupt(2), lightOn, CHANGE);
}

void loop()
{
  digitalWrite(9, HIGH);
  delay(2000);
  digitalWrite(9, LOW);
  delay(2000);
}

void lightOn(){
  buttonState = digitalRead(2);
  digitalWrite(7, buttonState);
}

reference:

No comments:

Post a Comment