Hello all!
I am trying to tackle a rather simple problem that could be done with the flip-flop. Indeed, if you think better way to approach this, do not hesitate to say!
It works to some extent... However, here the basic idea:
start with the q1 = 0, q 2 = 0 and ALS = 0
once light 1 on
Q1 = 1 and ALS = 1
accordingly
Q2 = 1
(after some time, have a look the timer) Q1 = 0
ALS = 1
wait till you get another proximity,
once proximity up,
light 2 off
Q2 = 0
ALS = 0
go back to the start !
code attached.
Please me know, what I am doing wrong ![]()
Any input much appreciated.
Best.
main loop
wave();
setLedState();
ledAction();
if (Sensor.getAmbient() > ALS_HT) {
ALS = 1;
}
else if (Sensor.getAmbient() < ALS_HT) {
ALS = 0;
}
if (myLedState == ledOn && ALS) {
//startcounting to turn off the ledOn
if (currentTime - startTime1 < 3000) {
Serial.println(currentTime - startTime1);
}
else {
startTime1 = currentTime;
// if (ALS) {
// myLedState2 = ledOn2;
// };
myLedState = ledOff;
}
myLedState2 = ledOn2;
myLedState = ledOff;
}
void wave() {
OS = Sensor.getProximity();
//above/below
switch (currentState) {
case WAITING_ABOVE:
if (OS > HT) {
belowHT = 0;
belowHT += currentTime - startTime;
startTime = currentTime;
//in fading toggle fadeUp
// if (myLedState == ledFading) {
// fadeUp = !fadeUp;
// }
currentState = WAITING_BELOW;
}
break;
case WAITING_BELOW:
if (OS < HT) {
aboveHT = 0;
aboveHT += currentTime - startTime;
startTime = currentTime;
currentState = WAITING_ABOVE;
//ALS = getALS();
}
break;
}
}
void setLedState() {
if ( (aboveHT > 1 && aboveHT < 350) && belowHT > 500 ) {
// if ledOn, turn
// ledOff 1
if ( myLedState == ledOn or myLedState == ledFading ) {
myLedState = ledOff;
}
if ( myLedState2 == ledOn2 or myLedState2 == ledFading2 ) {
myLedState2 = ledOff2;
}
//led on
else if (myLedState == ledOff) {
myLedState = ledOn;
}
//led on
else if (myLedState2 == ledOff2) {
myLedState2 = ledOn2;
}
aboveHT = 0;
belowHT = 0;
}
}
void ledAction() {
// turn led on// ORIGINAL
if (myLedState == ledOn ) {
analogWrite(ledPin, pgm_read_byte(&table[brightness]));
}
else if (myLedState2 == ledOn2) {
analogWrite(ledPin, pgm_read_byte(&table[brightness]));
}
//turn led off
else if (myLedState == ledOff) {
digitalWrite(ledPin, false);
}
//turn led2 off
else if (myLedState2 == ledOff2) {
digitalWrite(ledPin, false);
}
}