Hi,
thanks for the tip. I understand the concept, but I still get the same result, the light blinking at the interval.
And even stranger to me, is the output printed, this is without touching the switch at all:
(first int is the previous value of 'val', second is 'val' as gotten fresh from the digital input, third is the updated 'previousVal' as 'val')
Ready
1
0
0
writing HIGH
0
1
1
writing LOW
1
0
0
writing HIGH
0
1
1
writing LOW
1
0
0
writing HIGH
0
1
1
writing LOW
1
0
0
writing HIGH
0
1
1
writing LOW
1
0
0
writing HIGH
0
1
1
writing LOW
1
0
0
..... and so on.
Heres my updated code:
#include <Servo.h>
Servo servoMain; // Define our Servo
int ledPin = 13; // choose the pin for the LED
int inPin = 9; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int previousVal= 1;
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 2000; // interval at which to blink (milliseconds)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
servoMain.attach(5); // servo on digital pin 5
Serial.begin(9600); //Start Serial Communications with PC
Serial.println("Ready");
}
void loop(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
val = digitalRead(inPin); // read input value
if (val != previousVal) {
Serial.println(previousVal);
Serial.println(val);
previousVal= val;
Serial.println(previousVal);
if (val == LOW) { // check if the input is HIGH (button released)
digitalWrite(ledPin, HIGH); // turn LED OFF
// Run Servo
//servoMain.write(45);
Serial.println("writing HIGH");
previousMillis = currentMillis;
} else {
digitalWrite(ledPin, LOW); // turn LED ON
// Run Servo
//servoMain.write(0);
Serial.println("writing LOW");
}
}
}
}