I am trying to change the blink duration of blink with out delay with two switches. Here is what i have so far:
#define sw1 8
#define sw2 3
const int ledPin = 12; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long previousMillis2 = 0;
unsigned long ulintMillis = 0; // milliseconds since start
// 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 intervalO = 0; // interval at which to blink 3min 180000
long intervalx = 0; // interval at which to blink 3min 180000
int sw1State = 0;
int sw2State = 0;
void setup() {
// set the digital pin as output:
pinMode (sw1,INPUT);
pinMode (sw2,INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
sw1State = digitalRead(sw1);
sw2State = digitalRead(sw2);
unsigned long currentMillis = millis();
if (sw1State == HIGH)
{
intervalx = 5000;
intervalO = 1000;
}
if (sw2State == HIGH)
{
intervalx = 1000;
intervalO = 500;
}
if (sw1State || sw2State == HIGH)
if(currentMillis - previousMillis > intervalO)
{
previousMillis = currentMillis;
intervalO = intervalx - intervalO;
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
The problem is if the switch is turned off while the led is lit up it stays one. How can i remedy this?
Well i did that because without that statement the led is solid before the switch is tripped. What is the better way to do this. Should i assign values to the switch state to give a more clear conmand for:
sw1 on, sw2 off
sw1 on, sw2 on
sw1 off, sw2 off
ect
like this
1+1=2
1+0=1
0+1=1
0+0=0
then tell it to run the loop if (sw1+sw2>0)
#define sw1 8
#define sw2 3
const int ledPin = 12; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long previousMillis2 = 0;
unsigned long ulintMillis = 0; // milliseconds since start
// 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 intervalO = 0; // interval at which to blink 3min 180000
long intervalx = 0; // interval at which to blink 3min 180000
int sw1State = 0;
int sw2State = 0;
void setup() {
// set the digital pin as output:
pinMode (sw1,INPUT);
pinMode (sw2,INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
sw1State = digitalRead(sw1);
sw2State = digitalRead(sw2);
unsigned long currentMillis = millis();
if (sw1State == HIGH)
{
intervalx = 5000;
intervalO = 1000;
}
if (sw2State == HIGH)
{
intervalx = 1000;
intervalO = 500;
}
if ((sw1State + sw2State) >0)
if(currentMillis - previousMillis > intervalO)
{
previousMillis = currentMillis;
intervalO = intervalx - intervalO;
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
if ((sw1State + sw2State) == 0)
{
digitalWrite(ledPin, LOW);
}
}
well this was a test to make a blinking loop change with several different sensor inputs and variables. There is going to be more tan two i just figured if i can make two work the sky the limit. XD
OK, i merged this into my project i'm working on and it only turns on 3 min and off 3 min. It seems to be skipping this statement. I tried several different things, including removing one of the sw2State and im running out of ideas, any thought?
easterly81:
ok i went back to the drawing board of this one the orginal sketch does the same thing :0
would a switch case work better?
Yes.
Remember also that you have 4 possible states for your switches:
Both off, both on, 1 on and 2 off, 2 on and 1 off. Some states can be ignored if you don't want to use them.