so I wanted to create a simple LED game where there are 3 LEDs. 2 green and a red. The LEDs work in a loop and when the last red led is illuminated if a button is pressed then all lights turn of for 2 seconds and then the loop starts again but gets quicker. As a newbie I have made a pretty simple program using the delay function because it was easy... only there is one problem the Arduino will only recognize the button input AFTER the led has been illuminated and not WHILE the led is illuminated. The solution?.... Millis. The outcome? hours of banging my head against a desk trying to understand how it works with countless YouTube videos I kind of understand but i thought for a simple LED game like this it wouldn't be this hard. So basically I want to convert this code below to function with Millis instead of Delay!... And yes I did make the code in mBlock but I understand it.
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
float TIME = 0;
void _delay(float seconds) {
long endTime = millis() + seconds * 1000;
while(millis() < endTime) _loop();
}
void setup() {
pinMode(13,INPUT);
pinMode(2,INPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
TIME = 2;
while(1) {
if((digitalRead(13) == 1.000000) && (digitalRead(2) == 1.000000)){
digitalWrite(11,1);
digitalWrite(12,1);
digitalWrite(13,1);
_delay(2);
TIME += -0.02;
}else{
digitalWrite(11,1);
digitalWrite(12,0);
digitalWrite(13,0);
_delay(float(TIME));
digitalWrite(11,0);
digitalWrite(12,1);
digitalWrite(13,0);
_delay(float(TIME));
digitalWrite(11,0);
digitalWrite(12,0);
digitalWrite(13,1);
_delay(float(TIME));
}
_loop();
}
}
void _loop() {
}
void loop() {
_loop();
}
Your way of using/controlling loop is unnecessary complicated.
Make a test like this:
Turn on an LED in setup and set a timeout when the LED will be turned off.
In loop You check if millis() has passed that time. If it has, turn off the LED and set a new time for the LED to move.
Check for LED turn on time to pass.
I guess You might have looked at how to use millis().
Any help?
drmpf
June 7, 2021, 1:12am
3
Check out these two tutorials
How to write Timers and Delays in Arduino and
Multi-tasking in Arduino
Once you start using millis() rewriting your sketch as a series of tasks makes the process clearer.
digitalRead does not return a floating point value.
What is"mBlock"?
Hi @ardun10
try this sketch and learn how work.
RV mineirin
int seq = 0; // LEDs sequence
int timeD = 2000; // Fixed delay
unsigned long elapsed = 0; // Variable delay
//--------------------------------------------------------------------------------
void setup()
{
Serial.begin(115200);
pinMode(13, INPUT);
pinMode(2, INPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
elapsed = millis(); // Initialize variable delay
}
//--------------------------------------------------------------------------------
void myDelay()
{
if (millis() - elapsed >= timeD) // The delay time has passed
{
seq++; // Increments sequence
if (seq > 2) seq = 0; // If the sequence is longer than programmed restart sequence
elapsed = millis(); // Recharge variable delay
}
}
//--------------------------------------------------------------------------------
void loop()
{
if ((digitalRead(13) == HIGH) && (digitalRead(2) == HIGH))
{
digitalWrite(11, 1);
digitalWrite(12, 1);
digitalWrite(13, 1);
delay(2000); // Fixed delay
timeD -= 20; // Decrease variable delay 0,02 seconds
if (timeD < 0) timeD = 0; // If variable delay = 0 stay 0
Serial.println(timeD);
}
switch (seq)
{
case 0: // First sequence
{
digitalWrite(11, 1);
digitalWrite(12, 0);
digitalWrite(13, 0);
myDelay(); // Call variable delay
break;
}
case 1: // Second sequence
{
digitalWrite(11, 0);
digitalWrite(12, 1);
digitalWrite(13, 0);
myDelay(); // Call variable delay
break;
}
case 2: // Third sequence
{
digitalWrite(11, 0);
digitalWrite(12, 0);
digitalWrite(13, 1);
myDelay(); // Call variable delay
break;
}
default: // Any other case
break;
}
}
1 Like
This is exactly what I needed and the fact you added some statements in there helps so much. Your sketch is also really easy to understand. Honestly, your a legend!
system
Closed
October 5, 2021, 11:12pm
7
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.