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
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"
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;
}
}
}
}
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
@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.