int motorL1 = 2;
int motorL2 = 3;
int motorR1 = 5;
int motorR2 = 6;
int IRSensorLeft = 8;
int IRSensorRight = 9;
int IRSensor1 = 10;
int IRSensor2 = 11;
#define echopin A0
#define trigpin A1
int sensorLeft = 0;
int sensorRight = 0;
int sensor1 = 0;
int sensor2 = 0;
int touchPin = 12;
int buttonNew;
int buttonOld = 1;
int frontdist;
long duration;
int setdist = 50;
char id;
void setup()
{
pinMode(motorL1, OUTPUT);
pinMode(motorL2, OUTPUT);
pinMode(motorR1, OUTPUT);
pinMode(motorR2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
pinMode (trigpin, OUTPUT);
pinMode (echopin, INPUT);
pinMode (IRSensorLeft, INPUT);
pinMode (IRSensorRight, INPUT);
pinMode (IRSensor1, INPUT);
pinMode (IRSensor2, INPUT);
pinMode (touchPin, INPUT);
Serial.begin(9600);
}
void loop()
{
while (Serial.available()) // buttons for the application
{
id = Serial.read();
Serial.print(id);
}
switch (id)
{
case 'U': // when I press and read 'U' value from the application, the code below should run!
Start(); // this function will be called, this function allows the robot to move the motor for about 2 seconds, as the robot is in resting phase or stopping phase, meaning the IR attached on the robot is in the black line
Table1(); // after the robot moves outside the black line, check Start(), the motor will now move to the table 1 as stated, the motor will stop because I also indicated that if the IR detects a black line on the right side that is the table 1
Back(); // when the motor stops in table 1, there will be a button to send back the motor to the starting point, after pressingthe touchsensor this function will be called
break;
// same goes with this cases below
case 'D':
Start();
Table2();
Back();
break;
case 'L':
Start();
Table101();
Back();
break;
}
}
// this function makes the robot goes back to the original place, I used a touch sensor for this, so meaning, if the touch sensor detected a touch the robot will execute the code below
void Back()
{
buttonNew = digitalRead (touchPin);
if (buttonOld == 0 && buttonNew == 1)
{
Move();// this function will move the motor depending on the input of the IR sensor
Serial.print("Touched");
}
buttonOld = buttonNew;
}
//I want to run this function for 2 seconds to get out in its original place or the black line.
//Since I am using a line-tracing technology the starting point of the robot will be the 2 IR sensors placed in a black line for its stopping phase
//Now I want to start the robot until there's no black line and to move the motors to selected tables
//This is where I am having a prob.. I want to run this code for about 2 seconds only and afte the 2 seconds time iw should be continue to the next function/loop
void Start()
{
unsigned long DoMyLoopFor2000ms = 2000;
unsigned long HowLongHaveIBeenLoopy = millis();
while( (millis()-HowLongHaveIBeenLoopy) <= DoMyLoopFor2000ms )
{
sensorLeft = digitalRead (IRSensorLeft);
sensorRight = digitalRead (IRSensorRight);
if (sensorLeft == 1 || sensorRight == 1)
{
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
Serial.print("Forward 2 Second");
}
}
}
//functions to move to the specific tables
void Table1()
{
analogWrite(4, 255);
analogWrite(7, 255);
sensorLeft = digitalRead (IRSensorLeft); //input of the IR sensors
sensorRight = digitalRead (IRSensorRight); //input of the IR sensors
sensor1 = digitalRead (IRSensor1); //input of the IR sensors
frontdist = data(); // this will get the value of the long data() function below
Serial.println(frontdist);
if (frontdist > setdist) // when there is no obstacle the code below will be executed
{
Serial.print(id);
Move(); // this function will move the motor depending on the input of the IR sensor
if (sensor1 == 1) // Here the code where when this sensor sense a black block the motor will stop because that is the table 1
{
Stop(); // stop all the motors
}
}
else
{
Stop(); // when theres an obtacle the motot will stop
}
}
void Table2()
{
analogWrite(4, 255);
analogWrite(7, 255);
sensorLeft = digitalRead (IRSensorLeft);
sensorRight = digitalRead (IRSensorRight);
sensor2 = digitalRead (IRSensor2);
frontdist = data();
Serial.println(frontdist);
if (frontdist > setdist)
{
Serial.print(id);
Move();
if (sensor2 == 1)
{
Stop();
}
}
else
{
Stop();
}
}
void Table101()
{
analogWrite(4, 255);
analogWrite(7, 255);
sensorLeft = digitalRead (IRSensorLeft);
sensorRight = digitalRead (IRSensorRight);
sensor1 = digitalRead (IRSensor1);
sensor2 = digitalRead (IRSensor2);
frontdist = data();
Serial.println(frontdist);
if (frontdist > setdist)
{
Serial.print(id);
Move();
if (sensor1 == 1 && sensor2 == 1)
{
Stop();
}
}
else
{
Stop();
}
}
//Line-tracing movement
void Move()
{
if (sensorLeft == 0 && sensorRight == 0) // when it detects white surfeace the robot will move
{
Forward();
}
else if (sensorLeft == 1 && sensorRight == 0)
{
Right();
}
else if (sensorLeft == 0 && sensorRight == 1)
{
Left();
}
else if (sensorLeft == 1 && sensorRight == 1) // when detects a black line the robot will stop
{
Stop();
}
}
//Movement functions
void Forward()
{
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
}
void Left()
{
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, LOW);
}
void Right()
{
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
}
void Stop()
{
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, LOW);
}
// this will calculate the data to get the nearby object and to detect if there will be an obstacle
long data()
{
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
duration = pulseIn (echopin, HIGH);
return duration / 29 / 2;
}
Something like this perhaps.
if (id == 'U')
{
delay (3000);
for (unsigned long start = millis(); millis() - start < 3000;) Forward();
Table1();
}
Of course, this is "blocking code" but I suspect, because of your frequent use of delay(), that you have not reached a level of experience to understand the problems that blocking code causes.
This gets the last byte read from the monitor... which may depend heavily on what you have set as your "Line ending". .. probably set to "No line ending" to get it to accept a single character at a time.
Thanks for this. I will try this one.
Hello, guys Good day. I want to ask, Do you think this code will execute depending on the button that has been clicked??
void loop()
{
while (Serial.available()) // for buttons in app
{
id = Serial.read();
Serial.print(id);
}
switch (id)
{
case 'U':
for (unsigned long start = millis(); millis() - start < 2000;)
Start(); // I want to run this for 2 seconds only
Table1(); // move to specific tables
Back();
break;
case 'D':
for (unsigned long start = millis(); millis() - start < 2000;)
Start(); // I want to run this for 2 seconds only
Table2(); //move to specific tables
Back();
break;
case 'L':
for (unsigned long start = millis(); millis() - start < 2000;)
Start(); // I want to run this for 2 seconds only
Table101(); //move to specific tables
Back();
break;
}
}
//
void Back()
{
buttonNew = digitalRead (touchPin);
if (buttonOld == 0 && buttonNew == 1)
{
Move(); // go back to its original place
Serial.print("Touched");
}
buttonOld = buttonNew;
delay(2000);
}
//this is the function that will be called in switch
void Start()
{
sensorLeft = digitalRead (IRSensorLeft);
sensorRight = digitalRead (IRSensorRight);
if (sensorLeft == 1 && sensorRight == 1)
{
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
Serial.print("Forward 2 Second");
}
}
What happens when you try?
If you have millis() timing inside a 'for' loop, execution is "trapped" there, nothing outside the loop can happen, so it's not any better than a delay() call.
Would that be a problem for you?
This is silly. Just run it and see. If you do have problems, come back and explain what is wrong.
Ok sir thank you.. I will update this later..
Before you do, please read the forum instructions on how to post a question, how to post code. You are supposed to post the entire sketch, for one thing...
Another thing, you should test your code in manageable pieces. If you are not sure of your button code, don't test it it a medium sized untested program. Make a test program just for the button to verify that it works.
It's not possible to evaluate the effects of functions that are completely unknown to us, like 'Table101()'.
void loop() {
while (Serial.available()) { // for buttons in app
id = Serial.read();
Serial.print(id);
}
switch (id) {
case 'U':
Start(); // I want to run this for 2 seconds only
Table1(); // move to specific tables
Back();
break;
case 'D':
Start(); // I want to run this for 2 seconds only
Table2(); //move to specific tables
Back();
break;
case 'L':
Start(); // I want to run this for 2 seconds only
Table101(); //move to specific tables
Back();
break;
}
}
void Back() {
buttonNew = digitalRead (touchPin);
if (buttonOld == 0 && buttonNew == 1) {
Move(); // go back to its original place
Serial.print("Touched");
}
buttonOld = buttonNew;
delay(2000);
}
//this is the function that will be called in switch
void Start() {
if (digitalRead (IRSensorLeft) == 1 && digitalRead (IRSensorRight) == 1) {
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
Serial.print("Forward 2 Second");
}
}
Please note we need to see all your code not just a few functions.
Often errors are not where beginners think they are.
In this example you appear to have no way of doing nothing, once a letter has arrived it will keep doing that function over and over until you send another letter.
I will try this sir thank you, but I want to know if the Start function you created will run for 2 seconds or it will run until there is black line?
Ok sir I will try to edit this and update the code thank you.
Hello johnaqui
Take some time and descripe the desired functionality of the sketch in simple words and this very simple and short form, including all dependencies between inputs and outputs.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
Cross post reported.
The easiest way to test if it will work is to run the code.
The code you have posted does not compile because you did not post all code.
sketch.ino18 of 18 problems
'motorR2' was not declared in this scope
Wokwi - Online Arduino and ESP32 Simulator
Build failed!
sketch.ino: In function 'void loop()':
sketch.ino:5:5: error: 'id' was not declared in this scope
id = Serial.read();
^~
sketch.ino:9:11: error: 'id' was not declared in this scope
switch (id)
^~
sketch.ino:14:5: error: 'Table1' was not declared in this scope
Table1(); // move to specific tables
^~~~~~
sketch.ino:21:5: error: 'Table2' was not declared in this scope
Table2(); //move to specific tables
^~~~~~
sketch.ino:28:5: error: 'Table101' was not declared in this scope
Table101(); //move to specific tables
^~~~~~~~
sketch.ino: In function 'void Back()':
sketch.ino:38:3: error: 'buttonNew' was not declared in this scope
buttonNew = digitalRead (touchPin);
^~~~~~~~~
sketch.ino:38:28: error: 'touchPin' was not declared in this scope
buttonNew = digitalRead (touchPin);
^~~~~~~~
sketch.ino:39:7: error: 'buttonOld' was not declared in this scope
if (buttonOld == 0 && buttonNew == 1)
^~~~~~~~~
sketch.ino:41:5: error: 'Move' was not declared in this scope
Move(); // go back to its original place
^~~~
sketch.ino:41:5: note: suggested alternative: 'tone'
Move(); // go back to its original place
^~~~
tone
sketch.ino:44:3: error: 'buttonOld' was not declared in this scope
buttonOld = buttonNew;
^~~~~~~~~
sketch.ino: In function 'void Start()':
sketch.ino:51:3: error: 'sensorLeft' was not declared in this scope
sensorLeft = digitalRead (IRSensorLeft);
^~~~~~~~~~
sketch.ino:51:29: error: 'IRSensorLeft' was not declared in this scope
sensorLeft = digitalRead (IRSensorLeft);
^~~~~~~~~~~~
sketch.ino:52:3: error: 'sensorRight' was not declared in this scope
sensorRight = digitalRead (IRSensorRight);
^~~~~~~~~~~
sketch.ino:52:30: error: 'IRSensorRight' was not declared in this scope
sensorRight = digitalRead (IRSensorRight);
^~~~~~~~~~~~~
sketch.ino:56:18: error: 'motorL1' was not declared in this scope
digitalWrite(motorL1, HIGH);
^~~~~~~
sketch.ino:57:18: error: 'motorL2' was not declared in this scope
digitalWrite(motorL2, LOW);
^~~~~~~
sketch.ino:58:18: error: 'motorR1' was not declared in this scope
digitalWrite(motorR1, HIGH);
^~~~~~~
sketch.ino:59:18: error: 'motorR2' was not declared in this scope
digitalWrite(motorR2, LOW);
^~~~~~~
Error during build: exit status 1
That code will not run as it is missing the setup() function which is required under the Arduino IDE.
This is the whole code this post.. I mean I just shortened it because the code is too long.. but yeah thanks for noticing my post guys... I just want to know if in the switch statement.. the Start() func will run just for about 2 second as I set in for loop..
int motorL1 = 2;
int motorL2 = 3;
int motorR1 = 5;
int motorR2 = 6;
int IRSensorLeft = 8;
int IRSensorRight = 9;
int IRSensor1 = 10;
int IRSensor2 = 11;
#define echopin A0
#define trigpin A1
int sensorLeft = 0;
int sensorRight = 0;
int sensor1 = 0;
int sensor2 = 0;
int touchPin = 12;
int buttonNew;
int buttonOld = 1;
int frontdist;
long duration;
int setdist = 50;
char id;
void setup()
{
pinMode(motorL1, OUTPUT);
pinMode(motorL2, OUTPUT);
pinMode(motorR1, OUTPUT);
pinMode(motorR2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
pinMode (trigpin, OUTPUT);
pinMode (echopin, INPUT);
pinMode (IRSensorLeft, INPUT);
pinMode (IRSensorRight, INPUT);
pinMode (IRSensor1, INPUT);
pinMode (IRSensor2, INPUT);
pinMode (touchPin, INPUT);
Serial.begin(9600);
}
void loop()
{
while (Serial.available())
{
id = Serial.read();
Serial.print(id);
}
switch (id)
{
case 'U':
for (unsigned long start = millis(); millis() - start < 2000;)
Start();
Table1();
Back();
break;
case 'D':
for (unsigned long start = millis(); millis() - start < 2000;)
Start();
Table2();
Back();
break;
case 'L':
for (unsigned long start = millis(); millis() - start < 2000;)
Start();
Table101();
Back();
break;
}
}
void Back() // Conditional function will called in switch statement
{
buttonNew = digitalRead (touchPin);
if (buttonOld == 0 && buttonNew == 1)
{
Move();
Serial.print("Touched");
}
buttonOld = buttonNew;
delay(2000);
}
void Start()
{
sensorLeft = digitalRead (IRSensorLeft);
sensorRight = digitalRead (IRSensorRight);
if (sensorLeft == 1 && sensorRight == 1)
{
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
Serial.print("Forward 2 Second");
}
}
void Table1()
{
analogWrite(4, 255);
analogWrite(7, 255);
sensorLeft = digitalRead (IRSensorLeft);
sensorRight = digitalRead (IRSensorRight);
sensor1 = digitalRead (IRSensor1);
sensor2 = digitalRead (IRSensor2);
frontdist = data();
Serial.println(frontdist);
if (frontdist > setdist)
{
Serial.print(id);
Move();
if (sensor1 == 1)
{
Stop();
}
}
else
{
Stop();
}
}
void Table2()
{
analogWrite(4, 255);
analogWrite(7, 255);
sensorLeft = digitalRead (IRSensorLeft);
sensorRight = digitalRead (IRSensorRight);
sensor1 = digitalRead (IRSensor1);
sensor2 = digitalRead (IRSensor2);
frontdist = data();
Serial.println(frontdist);
if (frontdist > setdist)
{
Serial.print(id);
Move();
if (sensor2 == 1)
{
Stop();
}
}
else
{
Stop();
}
}
void Table101()
{
analogWrite(4, 255);
analogWrite(7, 255);
sensorLeft = digitalRead (IRSensorLeft);
sensorRight = digitalRead (IRSensorRight);
sensor1 = digitalRead (IRSensor1);
sensor2 = digitalRead (IRSensor2);
frontdist = data();
Serial.println(frontdist);
if (frontdist > setdist)
{
Serial.print(id);
Move();
if (sensor1 == 1 && sensor2 == 1)
{
Stop();
}
}
else
{
Stop();
}
}
void Move()
{
if (sensorLeft == 0 && sensorRight == 0)
{
Forward();
}
else if (sensorLeft == 1 && sensorRight == 0)
{
Right();
}
else if (sensorLeft == 0 && sensorRight == 1)
{
Left();
}
else if (sensorLeft == 1 && sensorRight == 1)
{
Stop();
}
}
void Forward()
{
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
}
void Left()
{
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, LOW);
}
void Right()
{
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
}
void Stop()
{
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, LOW);
}
long data()
{
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
duration = pulseIn (echopin, HIGH);
return duration / 29 / 2;
}
I doubt it.
why sir?? what seems the prob with the code