Hi everyone.
Have a hard time getting my code work.
I want to push a button, have an LED1 blink for 5 seconds and go off. I then want LED2 to come on immediately LED1 goes off.
My current Code is:
const long Timer = 6000;
void setup () {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(9, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis(); //***Delay on for LED 13 & 12
if (9 == HIGH){
if (currentMillis >= Timer){
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
} else{
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
}}
}
Thank you.
An immediate problem
if (9 == HIGH)
The number 9 is never going to be HIGH. You need to read the state of pin 9 using digitalRead() and test the state that is returned
Fix that first then look at other problems such as the fact that you never update the value of Timer so that once the program has been running for 6 seconds
currentMillis >= Timer
will always be true
made adjustments to the code and was able to fix the part of using pin 9 as input.
not been able to update the value of the timer so it still triggers after the timer for LED1 has expired
current code is...
const long Timer = 10000;
void setup () {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(9, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis(); //***Delay on for LED 13 & 12
if (digitalRead(9) == HIGH){
if (currentMillis >= Timer){
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
} else{
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
}}
}
mista_toni:
made adjustments to the code and was able to fix the part of using pin 9 as input.
not been able to update the value of the timer so it still triggers after the timer for LED1 has expired
current code is...
const long Timer = 6000;
void setup () {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(9, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis(); //***Delay on for LED 13 & 12
if (9 == HIGH){
if (currentMillis >= Timer){
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
} else{
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
}}
}
try this,
const long Timer = 1000;
unsigned long now = 0;
const byte button = 9;
void setup () {
Serial.begin(115200);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
if (digitalRead(button) == HIGH) {
if (millis() - now >= Timer) {
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
now = millis();
Serial.println("now");
} else {
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
}
}
}
edited: i put now in the wrong place
When does LED2 turn off ?
Start with the Blink Without Delay: https://www.arduino.cc/en/tutorial/BlinkWithoutDelay.
Then add a 'enable' variable to turn the blinking on and off. My example millis_within_millis.ino uses such a variable to turn the blinking on and off.
To do something for 5 seconds, that can be called a single shot timer. My example millis_single_delay.ino uses that.
When the blinking is done, turn off LED1 and turn on LED2.
I think there are about three ways to do this in a good way. Do this step by step. Try the examples, and try to change them to see what happens. Then start combining a few thing to work towards the result that you need.
Even an experienced programmer does not start to write the end result, but instead he or she starts with small pieces.
@ notsolowki: Code did not work when i tested it out.
mista_toni:
@ notsolowki: Code did not work when i tested it out.
What do you mean by didn't work?
Maybe this is what your looking for,
const long Timer = 1000;
unsigned long now = 0;
const byte button = 9;
bool state = false;
void setup () {
Serial.begin(115200);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
if (digitalRead(button) == HIGH) {
state = true;
now = millis();
}
if (state == true) {
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
if (millis() - now >= Timer) {
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
state = false;
}
}
}
Current Result of Code:
The Arduino is put on.... button is pushed and makes pin 9 high... LED1 comes on for 5 seconds. it goes off and then LED2 comes on.
Problem... if Arduino is plugged in and allowed to stay on for 6 seconds... when the button is pushed, LED1 flashed on and goes off, then LED2 comes on.
What i want is that no matter how long the arduino has been on, when the button is pushed, let LED1 come on for 5 seconds then go off and LED2 come on.
Current code...
const long Timer = 10000;
void setup () {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(9, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis(); //***Delay on for LED 13 & 12
if (digitalRead(9) == HIGH){
if (currentMillis >= Timer){
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
} else{
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
}}
}
This should turn on the led for 1 second then off,
const long Timer = 1000;
unsigned long now = 0;
const byte button = 9;
bool state = false;
void setup () {
Serial.begin(115200);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
if (digitalRead(button) == HIGH) {
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
now = millis();
state = true;
}
if (state == true) {
if (millis() - now >= Timer) {
Serial.println("off");
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
state = false;
}
}
}
you might want to change state to false at startup. do you want it to stay off when the cycle is complete or keep cycling indefinitely?
This sketch's loop doesn't run till Pin 4 is ticked to GND.
unsigned long changePoint;
const byte Phs1 = 2;
const byte Phs2 = 3;
bool mainPhs;
const unsigned long Passage = 30000UL;
unsigned long Change;
unsigned long nextChange;
unsigned long current;
byte startPB;
void setup()
{
pinMode(Phs1, OUTPUT);
pinMode(Phs2, OUTPUT);
pinMode(4,INPUT_PULLUP);
mainPhs = true;
digitalWrite(Phs1, HIGH);
digitalWrite(Phs2, LOW);
do
{
startPB = digitalRead(4);
}while (startPB == 1);
Change = millis();
nextChange = Change + 30000UL;
}
void loop()
{
current = millis();
if ((current - Change) >= 30000)
{
Change = nextChange;
nextChange = nextChange + 30000UL;
phaseChange();
}
}
void phaseChange ()
{
if(mainPhs)
{
digitalWrite(Phs1,LOW);
digitalWrite(Phs2,HIGH);
}
else
{
digitalWrite(Phs1,HIGH);
digitalWrite(Phs2,LOW);
}
mainPhs = !mainPhs;
}
UKHeliBob:
Fix that first then look at other problems such as the fact that you never update the value of Timer so that once the program has been running for 6 seconds
currentMillis >= Timer
will always be true
how do i update the value of the timer on my code?
I have lost track of exactly what you are trying to do
(1) What should happen when you turn on or reset the Arduino , ie which LEDs are on and off ?
(2) What should happen when the button becomes pressed ?
(3) What should happen when (2) is complete ?
(4) Should the LEDs turn off at the end of (4) ?
(5) Should it be possible to restart the set of actions without turning off or resetting the Arduino ?