type or paste code here[code]
#include <Servo.h> // https://www.arduino.cc/reference/en/libraries/servo/
#define m1 8
#define m2 9
#define EN1 2
#define m3 10
#define m4 11
#define EN2 3
#define m5 12
#define m6 13
#define EN3 4
#define m7 14
#define m8 15
#define EN4 5
#define m9 16
#define m10 17
#define EN5 6
#define STQ 20
#define Q4 53
#define Q3 52
#define Q2 51
#define Q1 50
Servo servo1;
#define ECHO 21
#define TRIG 22
long duration;
long distance;
void forward()
{
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
analogWrite(EN1, 255);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
analogWrite(EN2, 255);
digitalWrite(m5, HIGH);
digitalWrite(m6, LOW);
analogWrite(EN3, 255);
digitalWrite(m7, HIGH);
digitalWrite(m8, LOW);
analogWrite(EN4, 255);
}
void backward()
{
digitalWrite(m1, LOW);
digitalWrite(m2, HIGH);
analogWrite(EN1, 255);
digitalWrite(m3, LOW);
digitalWrite(m4, HIGH);
analogWrite(EN2, 255);
digitalWrite(m5, LOW);
digitalWrite(m6, HIGH);
analogWrite(EN3, 255);
digitalWrite(m7, LOW);
digitalWrite(m8, HIGH);
analogWrite(EN4, 255);
delay(1000);
}
void right()
{
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
analogWrite(EN1, 255);
digitalWrite(m3, LOW);
digitalWrite(m4, HIGH);
analogWrite(EN2, 175);
digitalWrite(m5, HIGH);
digitalWrite(m6, LOW);
analogWrite(EN3, 255);
digitalWrite(m7, LOW);
digitalWrite(m8, HIGH);
analogWrite(EN4, 175);
delay(1000);
}
void left()
{
digitalWrite(m1, LOW);
digitalWrite(m2, HIGH);
analogWrite(EN1, 175);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
analogWrite(EN2, 255);
digitalWrite(m5, LOW);
digitalWrite(m6, HIGH);
analogWrite(EN3, 175);
digitalWrite(m7, HIGH);
digitalWrite(m8, LOW);
analogWrite(EN4, 255);
delay(1000);
}
void stop()
{
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
analogWrite(EN1, 100);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
analogWrite(EN2, 100);
digitalWrite(m5, LOW);
digitalWrite(m6, LOW);
analogWrite(EN3, 100);
digitalWrite(m7, LOW);
digitalWrite(m8, LOW);
analogWrite(EN4, 100);
delay(1000);
}
void clockwise()
{
digitalWrite(m9, HIGH);
digitalWrite(m10, LOW);
analogWrite(EN5, 175);
delay(1000);
}
void counterclockwise()
{
digitalWrite(m9, LOW);
digitalWrite(m10, HIGH);
analogWrite(EN5, 175);
delay(1000);
}
void wait()
{
digitalWrite(m9, LOW);
digitalWrite(m10, LOW);
analogWrite(EN5, 100);
delay(1000);
}
void setup()
{
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(m4, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(m5, OUTPUT);
pinMode(m6, OUTPUT);
pinMode(EN3, OUTPUT);
pinMode(m7, OUTPUT);
pinMode(m8, OUTPUT);
pinMode(EN4, OUTPUT);
pinMode(m9, OUTPUT);
pinMode(m10, OUTPUT);
pinMode(EN5, OUTPUT);
pinMode(Q1, INPUT);
pinMode(Q2, INPUT);
pinMode(Q3, INPUT);
pinMode(Q4, INPUT);
pinMode(STQ, INPUT);
pinMode(ECHO, INPUT);
pinMode(TRIG, OUTPUT);
servo1.attach(23);
Serial.begin(9600);
}
void loop()
{
{
int temp1 = digitalRead(Q1);
int temp2 = digitalRead(Q2);
int temp3 = digitalRead(Q3);
int temp4 = digitalRead(Q4);
if (temp1 == 1 && temp2 == 0 && temp3 == 0 && temp4 == 0)
counterclockwise();
else if (temp1 == 0 && temp2 == 1 && temp3 == 0 && temp4 == 0)
forward();
else if (temp1 == 1 && temp2 == 1 && temp3 == 0 && temp4 == 0)
clockwise();
else if (temp1 == 0 && temp2 == 0 && temp3 == 1 && temp4 == 0)
left();
else if (temp1 == 1 && temp2 == 0 && temp3 == 1 && temp4 == 0)
stop();
else if (temp1 == 0 && temp2 == 1 && temp3 == 1 && temp4 == 0)
right();
else if (temp1 == 0 && temp2 == 0 && temp3 == 0 && temp4 == 1)
backward();
else if (temp1 == 0 && temp2 == 0 && temp3 == 0 && temp4 == 0)
wait();
}
{
ultra();
servo1.write(0);
if (distance <= 10)
{
sevo1.write(180);
}
{
servo1.write(90):
}
}
{
backward();
if (distance <= 10)
delay(1000);
}
void ultra()
{
digitalWrite(TRIG, LOW);
delay(2);
digitalWrite(TRIG, HIGH);
delay(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH);
distance = duration * 0.034 / 2;
}
{
for (int pos = 0; pos <= 180; pos += 1)
{
servo1.write(pos);
delay(15)
}
for (int pos = 180; pos >= 0; pos -= 1)
{
servo1.write(pos);
delay(15);
}
}
}
[/code]
I still find some errors
Hoping i will get help from you @StefanL38
Find your concern on me please
Lucky
make sure you indented the code in the IDE, that's done by pressing ctrlT on a PC or cmdT on a Mac
--> you'll see you are missing a closing } somewhere
why does the loop() function starts with two {{
void loop()
{
{
You wrote a sketch with more than 200 lines but without any comment. No one knows, what the sketch should do - even you will not know if you look at it in a few weeks.
Comments are mandatory for a program to understand.
There are many typos, function definiton at the wrong place and superfluous braces.
This compiles without errors, but I have no idea if it does what it should do.
#include <Servo.h> // https://www.arduino.cc/reference/en/libraries/servo/
#define m1 8
#define m2 9
#define EN1 2
#define m3 10
#define m4 11
#define EN2 3
#define m5 12
#define m6 13
#define EN3 4
#define m7 14
#define m8 15
#define EN4 5
#define m9 16
#define m10 17
#define EN5 6
#define STQ 20
#define Q4 53
#define Q3 52
#define Q2 51
#define Q1 50
Servo servo1;
#define ECHO 21
#define TRIG 22
long duration;
long distance;
void forward()
{
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
analogWrite(EN1, 255);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
analogWrite(EN2, 255);
digitalWrite(m5, HIGH);
digitalWrite(m6, LOW);
analogWrite(EN3, 255);
digitalWrite(m7, HIGH);
digitalWrite(m8, LOW);
analogWrite(EN4, 255);
}
void backward()
{
digitalWrite(m1, LOW);
digitalWrite(m2, HIGH);
analogWrite(EN1, 255);
digitalWrite(m3, LOW);
digitalWrite(m4, HIGH);
analogWrite(EN2, 255);
digitalWrite(m5, LOW);
digitalWrite(m6, HIGH);
analogWrite(EN3, 255);
digitalWrite(m7, LOW);
digitalWrite(m8, HIGH);
analogWrite(EN4, 255);
delay(1000);
}
void right()
{
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
analogWrite(EN1, 255);
digitalWrite(m3, LOW);
digitalWrite(m4, HIGH);
analogWrite(EN2, 175);
digitalWrite(m5, HIGH);
digitalWrite(m6, LOW);
analogWrite(EN3, 255);
digitalWrite(m7, LOW);
digitalWrite(m8, HIGH);
analogWrite(EN4, 175);
delay(1000);
}
void left()
{
digitalWrite(m1, LOW);
digitalWrite(m2, HIGH);
analogWrite(EN1, 175);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
analogWrite(EN2, 255);
digitalWrite(m5, LOW);
digitalWrite(m6, HIGH);
analogWrite(EN3, 175);
digitalWrite(m7, HIGH);
digitalWrite(m8, LOW);
analogWrite(EN4, 255);
delay(1000);
}
void stop()
{
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
analogWrite(EN1, 100);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
analogWrite(EN2, 100);
digitalWrite(m5, LOW);
digitalWrite(m6, LOW);
analogWrite(EN3, 100);
digitalWrite(m7, LOW);
digitalWrite(m8, LOW);
analogWrite(EN4, 100);
delay(1000);
}
void clockwise()
{
digitalWrite(m9, HIGH);
digitalWrite(m10, LOW);
analogWrite(EN5, 175);
delay(1000);
}
void counterclockwise()
{
digitalWrite(m9, LOW);
digitalWrite(m10, HIGH);
analogWrite(EN5, 175);
delay(1000);
}
void wait()
{
digitalWrite(m9, LOW);
digitalWrite(m10, LOW);
analogWrite(EN5, 100);
delay(1000);
}
void setup()
{
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(m4, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(m5, OUTPUT);
pinMode(m6, OUTPUT);
pinMode(EN3, OUTPUT);
pinMode(m7, OUTPUT);
pinMode(m8, OUTPUT);
pinMode(EN4, OUTPUT);
pinMode(m9, OUTPUT);
pinMode(m10, OUTPUT);
pinMode(EN5, OUTPUT);
pinMode(Q1, INPUT);
pinMode(Q2, INPUT);
pinMode(Q3, INPUT);
pinMode(Q4, INPUT);
pinMode(STQ, INPUT);
pinMode(ECHO, INPUT);
pinMode(TRIG, OUTPUT);
servo1.attach(23);
Serial.begin(9600);
}
void loop()
{
{
int temp1 = digitalRead(Q1);
int temp2 = digitalRead(Q2);
int temp3 = digitalRead(Q3);
int temp4 = digitalRead(Q4);
if (temp1 == 1 && temp2 == 0 && temp3 == 0 && temp4 == 0)
counterclockwise();
else if (temp1 == 0 && temp2 == 1 && temp3 == 0 && temp4 == 0)
forward();
else if (temp1 == 1 && temp2 == 1 && temp3 == 0 && temp4 == 0)
clockwise();
else if (temp1 == 0 && temp2 == 0 && temp3 == 1 && temp4 == 0)
left();
else if (temp1 == 1 && temp2 == 0 && temp3 == 1 && temp4 == 0)
stop();
else if (temp1 == 0 && temp2 == 1 && temp3 == 1 && temp4 == 0)
right();
else if (temp1 == 0 && temp2 == 0 && temp3 == 0 && temp4 == 1)
backward();
else if (temp1 == 0 && temp2 == 0 && temp3 == 0 && temp4 == 0)
wait();
}
{
ultra();
servo1.write(0);
if (distance <= 10)
{
servo1.write(180);
}
{
servo1.write(90);
}
}
{
backward();
if (distance <= 10)
delay(1000);
}
{
for (int pos = 0; pos <= 180; pos += 1)
{
servo1.write(pos);
delay(15);
}
for (int pos = 180; pos >= 0; pos -= 1)
{
servo1.write(pos);
delay(15);
}
}
}
void ultra()
{
digitalWrite(TRIG, LOW);
delay(2);
digitalWrite(TRIG, HIGH);
delay(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH);
distance = duration * 0.034 / 2;
}
My program Uses if , if-else as well as for statement ,
I was hoping it a sub-loop
Am i wrong ,should i remove it
Brother @J-M-L
what is a sub loop ?
stick to
if (condition) {
... // code if condition true
} else {
... // code if condition is false
}
don't add stupid {} just for the sake of it
Sir @MicroBahner
This is my first coding with such many line
I still learnt anything what should be learnt ,
i will tried to modified the code
With regard
lUCKY
The compiler-error-messages does contain information WHERE the error OCCURED
example
:211:5: error: expected ';' before '}' token
the "211" is the line of code
the 5 is the columm where the error occured
autoformatting code does all the indentions in a manner that makes it easy to discover
curly-brackets-errors
You have a lot of curly brackets that are not needed
There is a SINGLE opening bracket after
- void Nameoffunction {
if (your condition ) {
else {
for (.....) {
and a correspondig closing bracket
then you did define a function ultra() INSIDE of your function loop
this should be OUTSIDE of any other function.
And use at least a single EMPTY line between the closing bracket of a function and the begin of the next function
NOT this way
void forward()
{
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
analogWrite(EN1, 255);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
analogWrite(EN2, 255);
digitalWrite(m5, HIGH);
digitalWrite(m6, LOW);
analogWrite(EN3, 255);
digitalWrite(m7, HIGH);
digitalWrite(m8, LOW);
analogWrite(EN4, 255);
}
void backward()
{
digitalWrite(m1, LOW);
digitalWrite(m2, HIGH);
analogWrite(EN1, 255);
digitalWrite(m3, LOW);
digitalWrite(m4, HIGH);
analogWrite(EN2, 255);
digitalWrite(m5, LOW);
digitalWrite(m6, HIGH);
analogWrite(EN3, 255);
digitalWrite(m7, LOW);
digitalWrite(m8, HIGH);
analogWrite(EN4, 255);
delay(1000);
}
code it this way
void forward() { // put the curly bracket in the same line as the function name
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
analogWrite(EN1, 255);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
analogWrite(EN2, 255);
digitalWrite(m5, HIGH);
digitalWrite(m6, LOW);
analogWrite(EN3, 255);
digitalWrite(m7, HIGH);
digitalWrite(m8, LOW);
analogWrite(EN4, 255);
}
// empty line here
void backward() { // put the curly bracket in the same line as the function name
digitalWrite(m1, LOW);
digitalWrite(m2, HIGH);
analogWrite(EN1, 255);
digitalWrite(m3, LOW);
digitalWrite(m4, HIGH);
analogWrite(EN2, 255);
digitalWrite(m5, LOW);
digitalWrite(m6, HIGH);
analogWrite(EN3, 255);
digitalWrite(m7, LOW);
digitalWrite(m8, HIGH);
analogWrite(EN4, 255);
delay(1000);
}
best regards Stefan
Brother @StefanL38
Now my program remain with four errors i corrected some of the errors
C:\Users\user\Documents\Arduino\lucky_final_robot\lucky_final_robot.ino: In function 'void loop()':
lucky_final_robot:182:7: error: 'sevo1' was not declared in this scope
sevo1. write(180);
^~~~~
C:\Users\user\Documents\Arduino\lucky_final_robot\lucky_final_robot.ino:182:7: note: suggested alternative: 'servo1'
sevo1. write(180);
^~~~~
servo1
lucky_final_robot:185:24: error: expected ';' before ':' token
servo1. write(90):
^
lucky_final_robot:194:3: error: a function-definition is not allowed here before '{' token
{
^
lucky_final_robot:208:5: error: expected ';' before '}' token
}
^
exit status 1
'sevo1' was not declared in this scope
This are the errors remaining
Now You must be able to hep with me
Thanking you
the curly bracket in the same line as the function name is a personal preference. Some like to see vertically aligned brackets

That makes it all the more important to use comments. It's not just so that we can help better. It is first of all for you.
Edit: And you should not comment the syntax. Write what you expect the sketch to do at that point. And use meaningfull variable names. That will help a lot too.
And start step by step. Begin writing a small fuctionality and see if it works. Than extend step by step and test after each step.
It makes no sense to write so many lines without being able to test parts of them first. Making it to compile without errors is comparable easy to making it working correctly.
Did you try my posted code in #24 ? It should compile without errors. But only you can test if it works as you want it to.
i will tried it up ,Brother @MicroBahner
Hi,
- Are you programming for a Mega controller?
- What is your code supposed to do?
- What hardware are you having on the input and output of the controller?
Tom..
![]()
Make it a bit clear , i am not sure want you mean to advice me
But i am glad you suggest me something beyond
With regard
Lucky
Yes Brother,
I am programming for the mega 2560,which i am not very familiar of
I did with uno for some small project now going for extension i am struck any suggestion
Any suggestion welcome
with best regard
Lucky
Okay let me once edit it again
Brother @J-M-L
Hi,
This is the bit of code you hopefully have corrected, the format is wrong, there should be { and } in it
if (temp1 == 1 && temp2 == 0 && temp3 == 0 && temp4 == 0)
counterclockwise();
else if (temp1 == 0 && temp2 == 1 && temp3 == 0 && temp4 == 0)
forward();
else if (temp1 == 1 && temp2 == 1 && temp3 == 0 && temp4 == 0)
clockwise();
else if (temp1 == 0 && temp2 == 0 && temp3 == 1 && temp4 == 0)
left();
else if (temp1 == 1 && temp2 == 0 && temp3 == 1 && temp4 == 0)
stop();
else if (temp1 == 0 && temp2 == 1 && temp3 == 1 && temp4 == 0)
right();
else if (temp1 == 0 && temp2 == 0 && temp3 == 0 && temp4 == 1)
backward();
else if (temp1 == 0 && temp2 == 0 && temp3 == 0 && temp4 == 0)
Look at t his format;
if (pinFiveInput < 500)
{
// do Thing A
}
else if (pinFiveInput >= 1000)
{
// do Thing B
}
else
{
// do Thing C
}
- What is your code supposed to do?
- What hardware are you having on the input and output of the controller?
Tom...
![]()
My project is a dual tone multi frequency (DTMF).where the number keys in the phone will control the robot tank or penzer
2 for forward
4 for left
6 for right
5 to stop
8 for backward
and finally 1 ,3, 0 to control the moment of the turret of the tank
Hoping you have the enough information to help me out
Brother @TomGeorge
With regard
From Lucky
The ultrasonic sensor attach to the servo must stop the servo and M9 and M10 to have a low signal or must go to (wait)
Hi,
Have you edited your code with the recommendations about the if.. else format corrected?
Tom....
![]()