Boucle for in my code arduino run twice!

Hi everyone , this is part of code racing time system ..lazer point to sensor and when the bike cross the light .. modul gsm send message etc ...

in my code here i have only one problem .. boucle "for" run twice instead of run one time .. boucle for run just when the lazer is alligned to ldr sensor its just time to check the stability of the system ..
I JUST WANNA UNDERSTAND WHY BOUCLE FOR RUN TWICE :frowning:
if i point the lazer to sensor monitor show it alligned and start counting using boucle for .. if i take away the lazer before the count ends monitor go back to show me that the lazer not alligned .. the problem is when it s alligned i have to wait the boucle "for" run twice to show me that "waiting for the bike"

Help me please !

What is a "boucle"?

A loop.

However, I don't see one

Oh. Where is the code? (braces for post without tags...)

sorry guys .. this is the code
int x = 0 ;
int y = 0 ;
int i ;
int sensorPin = A1 ;
String input2;
int sensorValue = 0 ;
unsigned long prvmillis = 0 ;
bool alligned = false;
String input= "" ;

void setup() {
Serial.begin(9600) ;
Serial.println("WELCOME!");
for(i=0;i<=1;i++){
delay(1000);
Serial.println("POINT THE LAZER AND WRITE THE LETTRE 'a' TO START\n");
}
}
void loop(){
input2 = Serial.readStringUntil('\n');
if (input2 == "a"){
while(1) {
sensorValue = analogRead(sensorPin);
Serial.println(analogRead(sensorPin));
if (sensorValue <500) {
Serial.println("SYSTEM ALLIGNED");
alligned = true;
delay(10);
}
else {
Serial.println("SYSTEM NOT ALLIGNED POINT THE LAZE PLEAZE!");
alligned = false;

   delay(10); 
}
  sensorValue = analogRead(sensorPin);
 if(sensorValue <500)
  {
    for (int i = 0 ; i< 10 ; i ++){
      x = i ; 
      delay(500);
      Serial.println(x);
      Serial.println(sensorValue);
      sensorValue = analogRead(sensorPin);
     delay(250);
    }       
  }     
  if (alligned == true && x == 9){

Serial.print("WAITING FOR THE BIKE!\n");
break;
}
}
while (alligned == true){
// delay(1000);
// Serial.println(analogRead(sensorPin));
sensorValue = analogRead(sensorPin);
if (sensorValue > 500 )
{
Serial.println("THE BIKE CROSS THE LINE");
break;
}
}
}
}

You called it :wink:

Except for the mysterious part in the middle.... I see that quite often, wonder how that happens?

I think the forum software does it when it sees something it believe to be code. Clearly, it's not very good at it.

The forum software inserts code tags when there are more than 2 blank lines in code that is not in code tags. As for why it does that, who knows ?

@labidimohamed Please stop creating topics for this problem them deleting them

The second account that you created has been permanently suspended. Do not create another one

@ UKHeliBob

sorry i just post my question again with code correctly using ''' To appear as required .. but the forum banned me for 23 hours .. i try to fix this problem as fast as possible before the deadline set by teatcher

Think yourself lucky that your account has not been suspended as well

1 Like

Can't you do it in this thread?

@anon57585045
i solve the problem bro
look
i just delete( for loop) then i put it after the first condition

if(sensor<500) ```

and i change this

  if (alligned == true && x == 9){
Serial.print("WAITING FOR THE BIKE!\n");
break;
}

by this

 if (sensorValue <500 && x == 9){
        alligned = true ;
  Serial.print("WAITING FOR THE BIKE!\n");
 break;
             }

and its working fine .. this is the whole code with correction

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);
String textForSMS;
String f1001 = "+***********";
int x = 0  ;
int y = 0 ;
int i ;
int  sensorPin  =  A1 ;
String input2;
int sensorValue = 0 ;
unsigned long prvmillis = 0 ;
bool alligned = false;
String input= "" ;


 void setup() {
  Serial.begin(9600) ;
 mySerial.begin(9600);
    Serial.println("WELCOME!");
  for(i=0;i<=2;i++){
 

  delay(1000);
  Serial.println("POINT THE LAZER AND WRITE THE LETTRE 'a' TO START\n");
   }
 
   
   }
void loop(){
 
  input2 = Serial.readStringUntil('\n');
if (input2 == "a"){

while(1) {
    sensorValue = analogRead(sensorPin);
    Serial.println(analogRead(sensorPin));
    if (sensorValue <500) {
      Serial.println("SYSTEM ALLIGNED");
       Serial.println("SYSTEM ALLIGNED"); Serial.println("SYSTEM ALLIGNED");
 
        for (int i = 0 ; i< 10 ; i ++){
          x = i ;
          delay(500);
          Serial.println(x);
          Serial.println(sensorValue);
          sensorValue = analogRead(sensorPin);
         delay(250);
 
     
   
         
       sensorValue = analogRead(sensorPin);
   
        }
        }    else {
       Serial.println("SYSTEM NOT ALLIGNED POINT THE LAZE PLEAZE!");
       alligned = false;
       
       delay(10);
    }
      if (sensorValue <500 && x == 9){
        alligned = true ;
  Serial.print("WAITING FOR THE BIKE!\n");
 break;
             }
    
          }
       

           
while (alligned == true){
 
  sensorValue = analogRead(sensorPin);
    if (sensorValue > 500 )
    {
     Serial.println("Initializing...");
  delay(100);

  mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
  updateSerial();

   mySerial.println("AT+CSQ"); //Signal quality test, value range is 0-31 , 31 is the best
  updateSerial();
  mySerial.println("AT+CCID"); //Read SIM information to confirm whether the SIM is plugged
  updateSerial();
  mySerial.println("AT+CREG?");
updateSerial();
  mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
  updateSerial();
  mySerial.println("AT+CMGS=\"+***********\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
  updateSerial();
  mySerial.print("bike cross the line time 05:21"); //text content
  updateSerial();
  mySerial.write(26);
      break;
    }


}
}
}
void updateSerial()
{
  delay(500);
  while (Serial.available())
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available())
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

That's great! If you really want to impress the teacher, add some comment lines to explain how things work. Then remove redundant blank lines and any lines with multiple closing brackets, then auto-format the sketch in the IDE. It will make it much easier to read.

1 Like

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