Jump from one function to another

HI guys
i am begginer in writting code and arduino
i try to write this code

it's a code for (detection system motorcycle timming ) using ldr sensor to detect the motocycle when it cross the line so The four parts of the code are as follows

1/ make sure the sensor is alligned or not if its alligned( loop for ) counting 3 second just to be sure that the sensor is alligned
2/if the sensor is alligned the system ask me to write the number of bike
3/ if i write the number i go to waitting part // this is mean everything is ok the sensor is alligned and the number of bike is save so All that remains is the bike cross the line
4/ when the bike cross the line the part 4 excuted by what ever i want .. display timming on lcd on monitor printing ....

I JUST NEED HELP AND ADVICE HOW CAN I WRITE THIS CODE CORRECTLY I AM BEGGINER IN ARDUINO AND CODE .. TAKE A LOOK IN THE CODE .


#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include "LiquidCrystal.h"
const int RS = 34, EN = 36, D4 = 38, D5 = 40, D6 = 42, D7 = 44;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
int x = 0  ;
String inputString;
String num;
int i ;
int  sensorPin  =  A1 ;
int sensorValue = 0 ;
boolean alligned ;

boolean numb =false ;
const byte ROWS = 5; //five rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =  {
  {'q', '0', 't', 'i'},
  {'7', '8', '9', 'a'},
  {'4', '5', '6', 'z'},
  {'1', '2', '3', 'e'},
  {'y', 's', '#', '*'},
};
byte rowPins[ROWS] = {9, 10, 11, 12, 13}; //connect to the row pinouts of the keypad {11,10,9,8,7,6} // {10,9,8,7,6};
byte colPins[COLS] = {5, 6, 7, 8};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
Serial.begin(9600) ;
lcd.begin(16, 2);
Serial.println("WELCOME!");
delay(500);
Serial.println("POINT THE LAZER");
delay(500);
}

void loop() {
  sensorValue = analogRead(sensorPin);
  if (sensorValue <500 && numb == false) {    
     Serial.println("SYSTEM ALLIGNED");    
    Serial.println("Wait:");     
    sensorValue = analogRead(sensorPin);
        for (int i = 1 ; i> 0 ; i --){
          x = i ;
        Serial.println  (x); 
          delay(500);     
          sensorValue = analogRead(sensorPin);
         delay(250);     
       sensorValue = analogRead(sensorPin);
       alligned = true;   
      
        }     
        }    else {
       Serial.println("SYSTEM NOT ALLIGNED POINT THE LAZE PLEAZE!");   
           
       delay(700);     
    }
        sensorValue = analogRead(sensorPin);

while(alligned){
   
  Serial.println("enter the number of bike");         
char key = keypad.getKey();
 if (key != NO_KEY){  
  inputString += key; 
   Serial.println(key);
     if (key == '#') {    
        num = inputString.toInt(); 
       
        numb = true;
        break;              
}       
}   

    while (numb){
      sensorValue = analogRead(sensorPin);
Serial.println("WAITING FORT tHE BIKE!");

while(sensorValue >500){
  numb = false ;
Serial.println("part4 display timming on lcd ");
break;
  
}
  
}    

 } 




}

Hi Mohammed,

there is no need to SHOUT. Writing in CAPITAL letters is considered as shouting.
It seems that you are frustrated that your code does not yet work.

There are a few basic things you should do.
One thing is to use the autoformat-function of the Arduino-IDE.
It is very easy to use.
Press Ctrl-T and all code-indentions are done the exact right way

Please describe what your code does and how this differs from what you want the code to do

best regards Stefan

HI stefan

I am sorry if writing in capital seems to you like screaming no i don't mean this , thank you for the information about autoformat-fonction ,
A while ago I was working on the code and I solved the problem , but it still a little problem now in the code i hope you can help me to solve it ,
this is how the code is working

{
  sensorValue = analogRead(sensorPin);
  if (sensorValue < 500 && x != 1) {
    Serial.println("SYSTEM ALLIGNED");
    Serial.println("Wait:");
    sensorValue = analogRead(sensorPin);
    for (int i = 2 ; i > 0 ; i --) {
      x = i ;
      Serial.println  (x);
      delay(500);
      sensorValue = analogRead(sensorPin);
    }
  }
  else if (sensorValue > 500)  {
    Serial.println("SYSTEM NOT ALLIGNED POINT THE LAZE PLEAZE!");
    delay(700);
  }

this part of code the check if the sensor is alligned or not

  while (x == 1 && sensorValue < 500 ) {
    sensorValue = analogRead(sensorPin);
    Serial.println("enter the number of bike");
    delay(100);

    char key = keypad.getKey();
    if (key != NO_KEY) {
      inputString += key;
      Serial.println(key);
    }
    while (key == '#') {
      num = inputString.toInt();

      Serial.println("WAITING FORT tHE BIKE!");
      delay(100);
      sensorValue = analogRead(sensorPin);
      while (sensorValue > 500) {

        Serial.println("number of bike is :");
        Serial.println(num);
        timeofbike();

       // break;
      }
    }
    break ;
  }
}

void timeofbike()
{
  Serial.println("bike cross the line at : ");
}

the second part its working like this
if the sensor is alligned
so for loop starts counting just to check if the sensor is aligned or not

 for (int i = 2 ; i > 0 ; i --) {
      x = i ;
      Serial.println  (x);
      delay(500);
      sensorValue = analogRead(sensorPin);
    }

then if the sensor is aligned this part of code asks to enter the bike number

while (x == 1 && sensorValue < 500 ) {

    sensorValue = analogRead(sensorPin);
    Serial.println("enter the number of bike");
    delay(100);

then if I press # num store the number I write with the keyboard and the monitor displays "WAITING FORT tHE BIKE!"

char key = keypad.getKey();
    if (key != NO_KEY) {
      inputString += key;
      Serial.println(key);
    }
    while (key == '#') {
      num = inputString.toInt();

      Serial.println("WAITING FORT tHE BIKE!");
      delay(100);

and at the end of the code the most important part is to display the time and the participant number

 while (sensorValue > 500) {
        Serial.println("number of bike is :");
        Serial.println(num);
        timeofbike();

       // break;
      }

if only if the bike crosses the line in other words the sensor is not aligned, all these parts work well just the only problem now it's each time the code is executed and reached until the last part (display the time and the bike number) I want to write another number of another bike to display the time and number of other bike when they cross the line that's all, if you have an idea help me And thank you in advance :heart:

Below is the complete code without any cuts or explanations


#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include "LiquidCrystal.h"
const int RS = 34, EN = 36, D4 = 38, D5 = 40, D6 = 42, D7 = 44;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
int x = 0  ;
String inputString;
String num;
int i ;
int  sensorPin  =  A1 ;
int sensorValue = 0 ;



const byte ROWS = 5; //five rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =  {
  {'q', '0', 't', 'i'},
  {'7', '8', '9', 'a'},
  {'4', '5', '6', 'z'},
  {'1', '2', '3', 'e'},
  {'y', 's', '#', '*'},
};
byte rowPins[ROWS] = {9, 10, 11, 12, 13}; //connect to the row pinouts of the keypad {11,10,9,8,7,6} // {10,9,8,7,6};
byte colPins[COLS] = {5, 6, 7, 8};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
  Serial.begin(9600) ;
  lcd.begin(16, 2);
  Serial.println("WELCOME!");
  delay(500);
  Serial.println("POINT THE LAZER");
  delay(500);
}

void loop() {
  sensorValue = analogRead(sensorPin);
  if (sensorValue < 500 && x != 1) {
    Serial.println("SYSTEM ALLIGNED");
    Serial.println("Wait:");
    sensorValue = analogRead(sensorPin);
    for (int i = 2 ; i > 0 ; i --) {
      x = i ;
      Serial.println  (x);
      delay(500);
      sensorValue = analogRead(sensorPin);


    }

  }

  else if (sensorValue > 500) {
    Serial.println("SYSTEM NOT ALLIGNED POINT THE LAZE PLEAZE!");


    delay(700);
  }



  while (x == 1 && sensorValue < 500 ) {

    sensorValue = analogRead(sensorPin);
    Serial.println("enter the number of bike");
    delay(100);

    char key = keypad.getKey();
    if (key != NO_KEY) {
      inputString += key;
      Serial.println(key);

    }


    while (key == '#') {
      num = inputString.toInt();

      Serial.println("WAITING FORT tHE BIKE!");
      delay(100);
      sensorValue = analogRead(sensorPin);
      while (sensorValue > 500) {

        Serial.println("number of bike is :");
        Serial.println(num);
        timeofbike();

        // break;
      }
    }
    break ;
  }



}



void timeofbike()
{
  Serial.println("bike cross the line at : ");
}

Quote:
Short strings of words in capital letters appear bolder and "louder".

1 Like

I guess you would need another loop around your current program. Where you first add a name, then ride the ride then present the result, back to start...
Would it be better maybe to prepare a list of bikers beforehand?

Global variables should have longer names. i and x do not tell me what they are for. Also i and x are very likely to also be used as local variables. That is not forbidden, but might cause confusion... also there seems to be no need to have x and i as globals...

You might also remove some empty lines to make the code more compact.

Where is the bike time printed? I would expect in timeofbike() but it is not there...

Hi
In the race we are aware of the number of the contestant before he arrives minutes ago so we cannot make a list of the numbers of the contestants in order .

the loop for is just a counter, i the variable of this loop will count from 5 up to 1 in this period check the allignement if the counter reaches 1 and the sensor is alligner, ie pass to the other loop if not (the sensor is not alligner)the lcd display or the monitor will display that the sensor is not alligner
x will receive the i value each time and check it with the next loop condition

void timeofbike() is just an example of what i want as a result , i will change it the correct time later .

Do I understand this means, "I want to display the time and number of each bike when it crosses the finish line"?

what do you mean by aligned?

when i simulate your code, i need to force the sensor pin low (e.g. press a button connected to that pin) and then it drops into a loop that repeated prints "WAITING FORT tHE BIKE!"

i can understand that the alignment checks the sensor to have a value corresponding to a light beam hitting it. but once this is recognized, shouldn't the code only prompt for a bike # when the beam is broken and the sensor pin changes states and at the same time capture a timestamp.

also what happens if multiple bike cross the "finish" line close to one another or overlapping, so that the beam is broken only once.

consider -- sensor value may be opposite of what you expect


int  sensorPin  =  A1 ;

int  nBike;
char s [80];

// -----------------------------------------------------------------------------
void setup()
{
    Serial.begin(9600) ;

    while (500 < analogRead(sensorPin)) {
        Serial.println ("SYSTEM NOT ALLIGNED POINT THE LAZE PLEASE!");
        delay(1000);
    }
    Serial.println ("Ready");
}

// -----------------------------------------------------------------------------
void loop() {
    if (500 < analogRead(sensorPin))  {
        unsigned long msec = millis ();

        sprintf (s, "  %2ld:%02ld:%02ld.%03ld",
            msec / (3600L*1000), (msec / (60L*1000)) % 60,
            (msec / 1000) % 60, msec % 1000 );
        Serial.println (s);

        Serial.println ("Enter bike #: ");
        // add code to read bike # here

        // wait for bike to clear sensor
        while (500 < analogRead(sensorPin))
            ;
    }
}

Sorry, but still not clear...
What does it count?
Seconds? People?
And what does x stand for then?
People still in the race? Time left?

Give your variables a proper name!

Like countdown or peopleInRace...

This will yield self-documenting code that will save you a lot of explanation and me a lot of guessing.

Look at code Gc has written for you. No x, no i, much more clear...

before a few second of crossing the bike the line i write the number of bike so when it corss the line the sensor send signal to arduino so the time display on lcd .
it's an individual bike race based on the time the rider has traveled the specified distance

aligned mean light of laser directet at the sensor ,
it's an individual bike race based on the time the rider has traveled the specified distance
i will try the code .. thank you

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