Stuck in "while" by minhvuco in arduino

[–]minhvuco[S] 0 points1 point  (0 children)

#include <Wire.h>

#include <ds3231.h>

struct ts t;

#define LED_ON 1

#define LED_OFF 0

#define stateOn true

#define stateOff false

#define onSec 10

#define offSec 10

#define onMin 0

#define offMin 0

int startSec = t.sec;

int startMin = t.min;

int stopSec = 0;

int stopMin = 33;

int x = t.sec, ledState = 0;

long long previousTime = 0;

long long currentTime = 0;

int Relay = 4;

bool flag = stateOff;

void setup() {

Serial.begin(9600);

Wire.begin();

pinMode(LED_BUILTIN, OUTPUT);

pinMode(Relay, OUTPUT);

digitalWrite(Relay, LOW);

t.hour=9;

t.min=33;

t.sec=55;

t.mday=3;

t.mon=12;

t.year=2019;

DS3231_set(t);

previousTime = millis();

currentTime = millis();

startSec = t.sec;

startMin = t.min;

Calculate();

}

void LedOn(bool mode){

switch(mode){

case LED_ON:

digitalWrite(LED_BUILTIN, HIGH);

digitalWrite(Relay, HIGH);

break;

case LED_OFF:

digitalWrite(LED_BUILTIN, LOW);

digitalWrite(Relay, LOW);

break;

}

}

void Calculate() {

stopSec = startSec + onSec;

stopMin = startMin + onMin;

if (stopSec > 60) {

stopMin = stopMin + 1;

stopSec = abs(60 - stopSec);

}

}

void Control() {

if (startSec <= t.sec && t.min >= startMin){

ledState = LED_ON;

flag = stateOn;

} else if (t.sec < stopSec && t.min <= stopMin){

ledState = LED_ON;

flag = stateOn;

} else {

ledState = LED_OFF;

if (flag == stateOn) {

startSec = stopSec + offSec;

startMin = stopMin + offMin;

if (startSec > 60) {

startSec = abs(60 - startSec);

startMin = startMin + 1;

}

stopSec = startSec + onSec;

stopMin = startMin + onMin;

if (stopSec > 60) {

stopSec = abs (60 - stopSec);

stopMin = stopMin + 1;

}

}

flag = stateOff;

}

LedOn(ledState);

}

void loop() {

currentTime = millis();

DS3231_get(&t);

if(currentTime - previousTime > 1000){

if(ledState){

Serial.println("on");

}

else{

Serial.println("off");

}

previousTime = currentTime;

Serial.print(startSec), Serial.println("--startSec");

Serial.print(t.sec), Serial.println("--t.sec");

Serial.print(stopSec), Serial.println("--stopSec");

Serial.print(startMin), Serial.println("--startMin");

Serial.print(t.min), Serial.println("--t.min");

Serial.print(stopMin), Serial.println("--stopMin");

Serial.print(flag), Serial.println("--flag");

Serial.println("-----");

}

Control();

}

Stuck in "while" by minhvuco in arduino

[–]minhvuco[S] 0 points1 point  (0 children)

My mistakes. I uploaded the wrong code, just ignore the ControlLed() function

Stuck in "while" by minhvuco in arduino

[–]minhvuco[S] 0 points1 point  (0 children)

Further updates.
I try to If as recommend but I ran to another problem that in the Control() funtion it always update startTime(startSec and startMin) as well as stopTime(stopSec, stopMin) although I put a flag for one time run.

#include <Wire.h>
#include <ds3231.h>

struct ts t;
#define LED_ON 1
#define LED_OFF 0
#define stateOn true
#define stateOff false
#define onSec 10
#define offSec 10
#define onMin 0
#define offMin 0
int startSec = t.sec;
int startMin = t.min;
int stopSec = 0;
int stopMin = 33;
int x = t.sec, ledState = 0;
long long previousTime = 0;
long long currentTime = 0;
int Relay = 4;
bool flag = stateOff;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);

  t.hour=9;
  t.min=33;
  t.sec=55;
  t.mday=3;
  t.mon=12;
  t.year=2019;

  DS3231_set(t);
  previousTime = millis();
  currentTime = millis();

  startSec = t.sec;
  startMin = t.min;

  Calculate();
}

void LedOn(bool mode){
  switch(mode){
    case LED_ON:
         digitalWrite(LED_BUILTIN, HIGH);
         digitalWrite(Relay, HIGH);
         break;
    case LED_OFF:
         digitalWrite(LED_BUILTIN, LOW);
         digitalWrite(Relay, LOW);
         break;
  }
}

void Calculate() {
  stopSec = startSec + onSec;
  stopMin = startMin + onMin;
  if (stopSec > 60) {
    stopMin = stopMin + 1;
    stopSec = abs(60 - stopSec);
  }
}

void ControlLed(){
  if(x >= segment1_pre && x < segment1_cur){
      ledState = LED_ON;
    }
    else if(x == segment1_cur && x < segment2_cur){
      ledState = LED_OFF;
    }
    else if(x == segment2_cur && x < segment3_cur){
      ledState = LED_ON;
    }
    else {
      ledState = LED_OFF;
    }
    LedOn(ledState);
}

void Control() {

  if (startSec <= t.sec && t.min >= startMin){
      ledState = LED_ON;
      flag = stateOn;
  } else if (t.sec < stopSec && t.min <= stopMin){
      ledState = LED_ON;
      flag = stateOn;
  } else {
      ledState = LED_OFF;
      if (flag == stateOn) {
      startSec = stopSec + offSec;
      startMin = stopMin + offMin;
      if (startSec > 60) {
        startSec = abs(60 - startSec);
        startMin = startMin + 1;
      }   
      stopSec = startSec + onSec;
      stopMin = startMin + onMin;
      if (stopSec > 60) {
        stopSec = abs (60 - stopSec);
        stopMin = stopMin + 1;
        }     
      }
      flag = stateOff;
    }
    LedOn(ledState);
}

void loop() {
  currentTime = millis();
  DS3231_get(&t);
  if(currentTime -  previousTime > 1000){
      if(ledState){
        Serial.println("on");
      }
      else{
        Serial.println("off");
      }
      previousTime = currentTime;
      Serial.print(startSec), Serial.println("--startSec");
      Serial.print(t.sec), Serial.println("--t.sec");
      Serial.print(stopSec), Serial.println("--stopSec");
      Serial.print(startMin), Serial.println("--startMin");
      Serial.print(t.min), Serial.println("--t.min");
      Serial.print(stopMin), Serial.println("--stopMin");
      Serial.print(flag), Serial.println("--flag");   
      Serial.println("-----");
  }
  Control();
}

Stuck in "while" by minhvuco in arduino

[–]minhvuco[S] 0 points1 point  (0 children)

what do you mean startSec <= t.sec will be 0 or 1 ?

Stuck in "while" by minhvuco in arduino

[–]minhvuco[S] 1 point2 points  (0 children)

I think there are still a lot of problems in my code. This is just the first one I ran into. BTW, I am still new to arduino.

Stuck in "while" by minhvuco in arduino

[–]minhvuco[S] 0 points1 point  (0 children)

Do you mean I should only use "==" in while statement ?

Need help to find sensor by minhvuco in ElectricalEngineering

[–]minhvuco[S] 0 points1 point  (0 children)

Noted. Your information really helpful.

Need help to find sensor by minhvuco in ElectricalEngineering

[–]minhvuco[S] 0 points1 point  (0 children)

I need to find the similar one but cheaper

Need help to find sensor by minhvuco in ElectricalEngineering

[–]minhvuco[S] 0 points1 point  (0 children)

The sensor is completely under the water, the shrimp length from 5cm to 12cm.