How would i blink an led with a 1 second ON time and a variable OFF time controlled by a on/off switch. When the switch is 0, blink for 1 second every 1 min and when the switch reads 1, blink for 1 second every 2 min.
like using a DIP switch to do different things but in my case just one switch will be used.
Any help would be great thank you!
From the sticky "Read this before posting"
"Make an informative subject description, not "help me, I'm a noob", nor something in all capitals. Try to avoid saying "urgent". That's your problem, not ours."
I'd say you need to be looking at Arduino programming...
So i found this example code and it basically blinks an led. i can set the on time and also i can set the off time.
#define LED_PIN 13
#define LED_ON 1000 //milliseconds
#define LED_OFF 9000
unsigned long ms; //time from millis()
unsigned long msLast; //last time the LED changed state
boolean ledState; //current LED state
void setup(void)
{
pinMode(LED_PIN, OUTPUT);
}
void loop(void)
{
ms = millis();
blinkLED();
}
void blinkLED(void)
{
if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
digitalWrite(LED_PIN, ledState = !ledState);
msLast = ms;
}
}
How could i modify this to change the off time of the led? when the switch is low than led_off = 59000, and when switch is high led_off = 118000
Define a pin for your switch input
In your loop() read the state of the input
If it is HIGH set LED_OFF to 118000, else set it to 59000
evanmars:
Define a pin for your switch input
In your loop() read the state of the input
If it is HIGH set LED_OFF to 118000, else set it to 59000
Ok I got this far....
#define LED_PIN 13
#define SWITCH_PIN 2
#define LED_ON 1000 //milliseconds
#define LED_OFFI 59000
#define LED_OFFII 118000
unsigned long ms; //time from millis()
unsigned long msLast; //last time the LED changed state
boolean ledState; //current LED state
boolean switchState;
void setup(void)
{
pinMode(LED_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT);
Serial.begin(9600);
}
void loop(void)
{
ms = millis();
blinkLED();
}
void blinkLED(void)
{
switchState=digitalRead (SWITCH_PIN);
Serial.print(switchState);
if (ms - msLast > (ledState ? LED_ON : LED_OFFI)) {
digitalWrite(LED_PIN, ledState = !ledState);
msLast = ms;
}
}
How can I integrate switchState into my if statements?
EDIT: as of now it prints switchState to the serial monitor as 0 or 1.
if (switchState==1)
{
LED_OFF = 118000;
else
LED_OFF = 59000;
}
evanmars:
if (switchState==1)
{
LED_OFF = 118000;
else
LED_OFF = 59000;
}
I think this works!
#define LED_PIN 13
#define SWITCH_PIN 2
#define LED_ON 1000 //milliseconds
unsigned long LED_OFF;
unsigned long ms; //time from millis()
unsigned long msLast; //last time the LED changed state
boolean ledState; //current LED state
boolean switchState;
void setup(void)
{
pinMode(LED_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT);
Serial.begin(9600);
}
void loop(void)
{
ms = millis();
offTimeSet();
blinkLED();
}
void offTimeSet(void)
{
if (switchState == 1)
{
LED_OFF = 118000;
}
else
LED_OFF = 59000;
}
void blinkLED(void)
{
switchState = digitalRead (SWITCH_PIN);
Serial.print(switchState);
if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
digitalWrite(LED_PIN, ledState = !ledState);
msLast = ms;
}
}
Now to get more functionality out of it!
Ill be back with more questions....