#define LED 13 #define BUTTON 7 int val = 0; int stato = 0; void setup(){ pinMode(LED, OUTPUT); pinMode(BUTTON, INPUT); Serial.begin(9600); } void loop(){ if(digitalRead(BUTTON)==HIGH) digitalWrite(LED, HIGH); else digitalWrite(LED, LOW); }
Variante 2.
#define LED 13 #define BUTTON 7 int val = 0; int stato = 0; void setup(){ pinMode(LED, OUTPUT); pinMode(BUTTON, INPUT); Serial.begin(9600); } |
void loop(){ val = digitalRead(BUTTON); if(val == HIGH){ stato = 1 - stato; delay(100); } if(stato==1){ digitalWrite(LED, HIGH); } if(stato==0){ digitalWrite(LED, LOW); } Serial.println(stato); } |
Variante 3.
#define LED 13 #define BUTTON 7 int val = 0; int stato = 0; int vecchio = 0; void setup(){ pinMode(LED, OUTPUT); pinMode(BUTTON, INPUT); Serial.begin(9600); } void loop(){ val = digitalRead(BUTTON); if(val == HIGH && vecchio == LOW){ stato = 1 - stato; delay(100); } |
vecchio = val; if(stato==1){ digitalWrite(LED, HIGH); delay(200); digitalWrite(LED, LOW); delay(200); } else digitalWrite(LED, LOW); Serial.println(stato); } |