Hello all. I'm working on this code to blink a single LED on and off (the one called 'scan' in this code - the others are just static for now).
I found a tutorial here on how to do this with a fixed on and off time, without using the delay function which is very nice as eventually I will be doing multiple things with this code.
https://robojax.com/learn/arduino/?vid=robojax_millis_blink
I used his code as the basis for mine, and it works fine with the fixed on and off time. I tried then to re-define the on and off durations with each rotation of the loop so that it would stay on and off for different periods of time each time. But it doesn't seem to be doing that. Looking for some advice on what I might be missing.
#include <AltSoftSerial.h>
#include <DFRobotDFPlayerMini.h>
AltSoftSerial altserial;
DFRobotDFPlayerMini myDFPlayer;
int eye = 4;
int blaster = 2;
int scan = 3;
int ledpin =13;
int randNumber;
unsigned long offduration = 100;
unsigned long onduration = 1000;
int scanstate = HIGH;
long rememberTime = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(eye, OUTPUT);
pinMode(blaster, OUTPUT);
pinMode(scan, OUTPUT);
pinMode(ledpin, OUTPUT);
digitalWrite(eye, HIGH);
digitalWrite(blaster, HIGH);
digitalWrite(scan, scanstate);
}
void loop() {
// put your main code here, to run repeatedly:
scanblink();
}
void scanblink() {
if (scanstate == HIGH)
{
if ((millis() - rememberTime) >= onduration) {
scanstate = LOW;
rememberTime = millis();
}
}
else
{
if ( (millis()- rememberTime) >= offduration) {
scanstate = HIGH;
rememberTime=millis();
}
}
digitalWrite (scan, scanstate);
onduration = random(100, 2000);
offduration = random (500, 1000);
}