Ragazzi..che ne dite di questo sketch??
Dovrebbe accendere un motore DC tramite pulsante.
const int buttonPin = 14; // the number of the pushbutton pin
const int MotorPin = 9; // the number of the motor pin
// Variables will change:
int MotorState = LOW; // the current state of the output pin
int buttonState = LOW; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 250; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(MotorPin, OUTPUT);
// set initial Motor state
digitalWrite(MotorPin, MotorState);
}
void loop() {
buttonState = digitalRead(buttonPin);
if( (millis() - lastDebounceTime) > debounceDelay) {
if ( (buttonState == HIGH) && (MotorState = LOW) ){
digitalWrite(MotorPin,HIGH);
MotorState = HIGH;
lastDebounceTime = millis();
}
if ( (buttonState == HIGH) && (MotorState = HIGH) ){
digitalWrite (MotorPin,MotorState);
MotorState = HIGH;
lastDebounceTime = millis();
}
if ( (buttonState ==LOW) && (MotorState = LOW) ){
digitalWrite(MotorPin,MotorState);
MotorState = LOW;
lastDebounceTime = millis();
}
if ( (buttonState ==LOW) && (MotorState = HIGH) ){
digitalWrite(MotorPin,LOW);
MotorState = LOW;
lastDebounceTime = millis();
}
}
}