Interrupting a cycle depending on a value given by a sensor

Hello guys! I am a newbie in this world, so please be kind :slight_smile:
We have this program, how we stop "initiere_ciclu_pietoni" if at some time in running, the value of "flex" is above 200?

const int led_verde_pieton = 2;   
const int led_rosu_pieton = 3;  
const int led_verde_vehicule = 4;   
const int led_galben_vehicule = 5;  
const int led_rosu_vehicule = 6; 
const int led_alb = 8;
const int t_galben = 2000;      
const int t_siguranta_1 = 2000;     
const int t_siguranta_2 = 2000;  
const int t_traversare = 4000; 
const int t_intermitent = 400;   
unsigned long t_min_ciclu_vehicule = 10000;
unsigned long t_ultimul_ciclu_pietoni = 0; 
const int flex = analogRead(0);

void setup() {
  Serial.begin(9600);
  pinMode(led_verde_pieton, OUTPUT);  
  pinMode(led_rosu_pieton, OUTPUT);  
  pinMode(led_verde_vehicule, OUTPUT);  
  pinMode(led_galben_vehicule, OUTPUT);  
  pinMode(led_rosu_vehicule, OUTPUT);    
  pinMode(led_alb,OUTPUT);
  digitalWrite(led_verde_vehicule, HIGH);
  digitalWrite(led_rosu_pieton, HIGH);   
}


void loop() 
{
  const int flex = analogRead(0);
  Serial.print("Valoare flex: ");
  Serial.println(flex, DEC);
  if (flex < 200 && millis() - t_ultimul_ciclu_pietoni < t_min_ciclu_vehicule) 
  { 
    delay(t_min_ciclu_vehicule - (millis() - t_ultimul_ciclu_pietoni));   
    initiere_ciclu_pietoni();      
  }
  if (flex < 200 && millis() - t_ultimul_ciclu_pietoni > t_min_ciclu_vehicule) 
  { 
    initiere_ciclu_pietoni();     
  }
}
  void initiere_ciclu_pietoni() 
{     
  digitalWrite(led_verde_vehicule, LOW);   
  digitalWrite(led_galben_vehicule, HIGH); 
  delay(t_galben);    
  digitalWrite(led_galben_vehicule, LOW);  
  digitalWrite(led_rosu_vehicule, HIGH); 
  delay(t_siguranta_1);   
  digitalWrite(led_rosu_pieton, LOW); 
  digitalWrite(led_verde_pieton, HIGH); 
  delay(t_traversare);  
  for (int x = 0; x < 5; x = x + 1) 
  { 
    digitalWrite(led_verde_pieton, LOW); 
    delay(t_intermitent);   
    digitalWrite(led_verde_pieton, HIGH); 
    delay(t_intermitent);  
  }
  digitalWrite(led_verde_pieton, LOW);
  digitalWrite(led_rosu_pieton, HIGH);  
  delay(t_siguranta_2);       
  digitalWrite(led_rosu_vehicule, LOW);  
  digitalWrite(led_verde_vehicule, HIGH); 
  t_ultimul_ciclu_pietoni = millis();  
}

Thank you very very much!

Well, you can't since "flex" is in another function. Make flex global and then test it's value in the initiere_ciclu_pietoni function.

1 Like

Your topic was MOVED to its current forum category as it is more suitable than the original

1 Like

If I counted correctly there are about 18 seconds worth of delays in the initiere_ciclu_pietoni() function. You can check the analog during the initiere_ciclu_pietoni() function but it won't be any more responsive than your shortest delay (400ms) and may take up to 4 seconds to respond. If you want it to immediately stop when the analog is more than 200 then you need to restructure your code to use only millis() calls and a state machine to implement the LED pattern in the initiere_ciclu_pietoni() function.

1 Like

Besides what @ToddL1962 recommended, remove the declaration of flex from void()

And change this

to this

int flex = analogRead(0); <== not "const"

so it becomes a global variable that you can change and interrogate from everywhere in your sketch

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.