I just started working with my arduino and built some simple programs led projects, but what I want to do next is more complex and I don't know where to start.
What I want to do next is build a LED timer. When you push the button the LED light remains on for a determined amout of time (say 10 seconds) and then shuts off and stays off. The determined amount of time then can be changed thru a push button switch up/down in seconds etc..
Here is where I am at now with a basic momentary LED on when button is pushed.
int ledPin = 12; // LED is connected to pin 12
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
if (val == LOW) { // check if the button is pressed
digitalWrite(ledPin, HIGH); // turn LED on
}
if (val == HIGH) { // check if the button is not pressed
digitalWrite(ledPin, LOW); // turn LED off
}
}
void loop(){
// add this
int delayTime = (analogRead (A0)*10); // delay up to 10.24 seconds
val = digitalRead(switchPin); // read input value and store it in val
if (val == LOW) { // check if the button is pressed
digitalWrite(ledPin, HIGH); // turn LED on
delay (delayTime); // keep the LED on
}
I updated my code and added print function to show resistance in pot.. I was trying to use millis but I did not know how to implement the millis code. I will test it tonight
int ledPin = 12; // LED is connected to pin 12
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
int analogPin = 0; // variable pots impute to control time via resistance
void setup() {
Serial.begin(9600); //begin serial communications lcd display
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
}
void loop(){
int sensorValue = analogRead(A0); // store resistance value
Serial.println(sensorValue, DEC);
int delayTime = (analogRead (A0)*10); // delay up to 10.24 seconds
val = digitalRead(switchPin); // read input value and store it in val
if (val == LOW) { // check if the button is pressed
digitalWrite(ledPin, HIGH); // turn LED on
delay (delayTime); // keep the LED on
}
if (val == HIGH) { // check if the button is not pressed
digitalWrite(ledPin, LOW); // turn LED off
}
}