Hello,
I am working on a program where I cannot stop the processor, thus I have to use millis. In one scenario, I want an output to flash on/off x amount of times. I tried putting a for loop around the on/off outputs but it seems to continuously reset the millis.
Excuse the name in my codes, it's for pneumatic props.
If button pressed
turn on output for x seconds
turn off output for x seconds
repeat two more times
//2. HORSE ONE PNEUMATIC POP UP
if(digitalRead(horseOneIn) == LOW && horseOneReady){ //if target is hit
Serial.print("3");
horseOneOn = true;
horseOneReady = false;
horseOnMillis = currentMillis;
}
for(int i = 0; i < 4; i++){
if(horseOneOn){
digitalWrite(horseOneOut, HIGH);
Serial.print("*4-");
if((currentMillis - horseOnMillis) >= horseOnDelay){
Serial.print("you made it");
horseOneOn = false;
digitalWrite(horseOneOut, LOW);
horseOneOff = true;
}
}
if(horseOneOff){
horseOneOff = currentMillis;
Serial.print("horseOFF");
if((currentMillis - horseOneOff) >= horseOffDelay){
Serial.print("DONEONDONEONDONE*#*(@(!(#DONEONEONEON");
horseOneReady = true;
}
}
}
Always post ALL the code.
Unless currentMillis is updated somewhere, the posted code won't work as intended.
I recommend this tutorial: https://www.baldengineer.com/blink-without-delay-explained.html
Think it through…
How will horseOneOff ever trigger the true clause ?
horseOneOff appears to be defined as a bool - it is assigned the value true. Not sure how this arithmetic would evaluate even if the compiler allows it. You can't subtract true/false from currentMillis() which is an unsigned long value. Did you perhaps mean:
if((currentMillis - horseOffMillis) >= horseOffDelay){
assuming a variable called horseOffMillis is actually defined? (would need to see rest of code)
(beat to it by lastchancename I see!)
Ahh, thank you all. I noticed a big syntax error with horseOneOff.
I am going to grind at it a bit more, fix the obvious errors, and update with full code.
OKAY, SO!!!!!
I was hyperfocus on using a For Loop when all I really needed was it to repeat only X amount of times. I used the Count function and got it to work! See full code below. Thanks for the help!
#include <AltSoftSerial.h>
#include <SPI.h>
#include <wavTrigger.h>
#include <Controllino.h> //
//INPUTSSSSS
const int clownOneIn = CONTROLLINO_A0;
const int horseOneIn = CONTROLLINO_A1;
const int horseTwoIn = CONTROLLINO_A2;
const int clownCarIn = CONTROLLINO_A3;
const int cannonIn = CONTROLLINO_A4;
const int lionCageOneIn = CONTROLLINO_A5;
const int lionCageTwoIn = CONTROLLINO_A6;
const int beardedOneIn = CONTROLLINO_A7;
const int beardedTwoIn = CONTROLLINO_A8;
const int clownTwoIn = CONTROLLINO_A9;
const int deathMonkeyIn = CONTROLLINO_IN0;
//OUTPUTSSS
const int clownOneOut = CONTROLLINO_D0; //LED Eyes
const int horseOneOut = CONTROLLINO_D1;
const int horseTwoOut = CONTROLLINO_D2;
const int clownCarOut = CONTROLLINO_D3;
const int cannonOut = CONTROLLINO_D4;
const int lionCageOneOut = CONTROLLINO_D5;
const int lionCageTwoOut = CONTROLLINO_D6;
const int beardedOneOut = CONTROLLINO_D7;
const int beardedTwoOut = CONTROLLINO_D8;
const int clownTwoOut = CONTROLLINO_D9;
const int deathMonkeyOut = CONTROLLINO_D10;
//TIMERSSSS
//CLOWN HEAD ONE
unsigned long clownMillis = 0;
unsigned long eyesDelay = 3000;
bool clownReady = false;
//HORSE ONE
unsigned long horseOneMillis = 0;
unsigned long horseTime = 0;
bool horseOneOn = false;
boolean horseOutState = 0;
int horseOneCount = 0;
void setup() {
Serial.begin(9600);
//INPUTSSSS
pinMode(clownOneIn, INPUT);
pinMode(horseOneIn, INPUT);
pinMode(horseTwoIn, INPUT);
pinMode(clownCarIn, INPUT);
pinMode(cannonIn, INPUT);
pinMode(lionCageOneIn, INPUT);
pinMode(lionCageTwoIn, INPUT);
pinMode(beardedOneIn, INPUT);
pinMode(beardedTwoIn, INPUT);
pinMode(clownTwoIn, INPUT);
pinMode(deathMonkeyIn, INPUT);
//OUTPUTSSSS
pinMode(clownOneOut, OUTPUT);
pinMode(horseOneOut, OUTPUT);
pinMode(horseTwoOut, OUTPUT);
pinMode(clownCarOut, OUTPUT);
pinMode(cannonOut, OUTPUT);
pinMode(lionCageOneOut, OUTPUT);
pinMode(lionCageTwoOut, OUTPUT);
pinMode(beardedOneOut, OUTPUT);
pinMode(beardedTwoOut, OUTPUT);
pinMode(clownTwoOut, OUTPUT);
pinMode(deathMonkeyOut, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
//1. CENTERPIECE CLOWN HEAD
if ((digitalRead(clownOneIn)) == LOW) {
clownMillis = currentMillis;
digitalWrite(clownOneOut, HIGH); //turn on LED eyes
clownReady = true;
Serial.print("1");
}
if (clownReady) {
if ((currentMillis - clownMillis) >= eyesDelay) {
clownReady = false;
digitalWrite(clownOneOut, LOW);
Serial.print("2");
}
}
//2. HORSE ONE PNEUMATIC POP UP
if (digitalRead(horseOneIn) == LOW) {
delay(50);
horseOneCount = 0;
horseOneOn = true;
}
if (horseOneOn) {
if (currentMillis - horseOneMillis >= 300) {
if (horseOneCount < 4) {
horseOneMillis = currentMillis;
horseOutState = !horseOutState;
Serial.print(" State:");
Serial.print(horseOutState);
digitalWrite(horseOneOut, horseOutState);
horseOneCount++;
Serial.print(" Count");
Serial.print(horseOneCount);
}
else if (horseOneCount >= 4) {
digitalWrite(horseOneOut, LOW);
horseOutState = 0;
horseOneCount = 0;
horseOneOn = false;
horseOneMillis = 0;
Serial.println(" Count Zero ");
}
}
}
}