Go Down

Topic: why code didn't work ? (Read 857 times) previous topic - next topic

ahmedashraf30515

Nov 09, 2017, 11:03 pm Last Edit: Nov 13, 2017, 05:33 pm by ahmedashraf30515
iam trying to write simple autoparking code 
1-- sevo                 type   S3003       
1--ultrasonic           type   HC-SR04     
1--motor driver       type   l298
......................................................
#include <Servo.h>
Servo servo;
int d; //distance variable for ultrasonic
long duration; //time duration of ultrsonic wave
//motor 1
int en1=11;//speed of motor 1
int M11=7;
int M12=8;
// motor 2
int en2=3;//speed of motor 2
int M21=2;
int M22=4;
......................................................
my code is made up of a group of functions
motor control function:   forward()   back()   Fleft()    Fright()     ..........and so on

ultasonic and servo function which take angle and return distance at this angle --int distance(int angle)

and iam using serial to control the car as shown
..................................................................
void loop() {
 g=Serial.read();
   if (g=='1'){
 forward();}
 else if(g=='2'){
   back();}
 else if(g=='3'){
   Fleft();}
   else if(g=='4'){
  Fright();}
   else if(g=='5'){
  Stop();}
   else if(g=='6'){

   sright();}   

   else if(g=='7'){
   Bright();}
else if(g=='8'){
  spinL();}
else if(g=='9'){
   spinR();}
}
............................................................
i use serial only to test the code and I will cancel it later
the problem is :
when i upload the full code to arduino the only function that work is  distance();
at first i think the problem was with the connection  so i upload only  half the code that contain the motor functions and i try to control my car with serial .......it work?that is good
and when i upload the part that have distance() function with small modify so that i could see the distance at serial monitor and it work too????
.................................................
i supply the servo from dc adaptor+powersupply circuit which me  500mA and 5v i will use battery letar
the motor driver from two battery 3.7v 2000mA rechargable
the ultrasonic from arduino 5v pin
and arduino from USB cable
.................................................
what is the problem with my code why he didn't work when i use newping and servo liberary
i search about the liberaries the i found that servo work with timer 1 and newping with timer 2
and delay() use timer 0 for interupt is that the problem of my code ?????? and if what sould i do?
I hope I have explained my problem well
thanks
Code: [Select]

#include <Servo.h>
Servo servo;
int test;

//motor 1
int en1=11;//speed of motor 1
int M11=7;
int M12=8;
// motor 2
int en2=3; //speed of motor 2
int M21=2;
int M22=4;
int spee=100;
char g;
void setup() {
  servo.attach(10);
  pinMode(en1,OUTPUT);
pinMode(M11,OUTPUT);
pinMode(M12,OUTPUT);
pinMode(en2,OUTPUT);
pinMode(M21,OUTPUT);
pinMode(M22,OUTPUT);
Serial.begin(9600);
  // put your setup code here, to run once:

}

void loop() {
  g=Serial.read();
    if (g=='1'){
  forward();}
  else if(g=='2'){
    back();}
  else if(g=='3'){
    Fleft();}
    else if(g=='4'){
   Fright();}
    else if(g=='5'){
   Stop();}
    else if(g=='6'){
    step1();}
    else if(g=='7'){
    Bright();}
 else if(g=='8'){
   spinL();}
 else if(g=='9'){
    spinR();}

}

A.A.K ;)

Robin2

Please post your complete program.

And please post your program using the code button </>
Code: [Select]
so it looks like this. See How to use the Forum.  It makes it much easier for people to help you

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ahmedashraf30515

 I have modified the post my code  is still under construction but unfortunately I can not complete it because of this problem I hope you can help me and thank you for help
A.A.K ;)

Robin2

i use serial only to test the code and I will cancel it later
the problem is :
when i upload the full code to arduino the only function that work is  distance();
at first i think the problem was with the connection  so i upload only  half the code that contain the motor functions and i try to control my car with serial .......it work?that is good
and when i upload the part that have distance() function with small modify so that i could see the distance at serial monitor and it work too????

I don't really understand what you are trying to say here.

It seems like the motor control works OK from Serial if you don't have the newPing library included

But when newPing library is included you can't control the motors, but you do get correct distance readings.

Is that correct?

The PWM pins 3 and 11 use Timer2. Try using pins 5 and 6 (Timer 0) for your motor speed control and change newPing to pins 3 and 11. However I have no experience of the newPing library.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ahmedashraf30515

I don't really understand what you are trying to say here.

It seems like the motor control works OK from Serial if you don't have the newPing library included

But when newPing library is included you can't control the motors, but you do get correct distance readings.

Is that correct?

The PWM pins 3 and 11 use Timer2. Try using pins 5 and 6 (Timer 0) for your motor speed control and change newPing to pins 3 and 11. However I have no experience of the newPing library.

...R
yes that's what i mean but i have small question if i use pin 5 and 6 for motor speed, as i known delay() use (timer0) do you think that will make problem too with control??
thanks for your help Robin
A.A.K ;)

Robin2

You can safely use PWM on pins 5 and 6 without affecting the way millis() and micros() and delay() work.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ahmedashraf30515

didn't work too????
but when i move this part of code
Code: [Select]

g=Serial.read();
    if (g=='1'){
  forward();}
  else if(g=='2'){
    back();}
  else if(g=='3'){
    Fleft();}
    else if(g=='4'){
   Fright();}
    else if(g=='5'){
   Stop();}
    else if(g=='6'){
    step1();}
    else if(g=='7'){
    Bright();}
 else if(g=='8'){
   spinL();}
 else if(g=='9'){
    spinR();}
//    else if(g=='a'){
//   Step2();}

and repalce it by some functions and delays as shown i was just testing it work well and give me reading at serial monitor
Code: [Select]

 test=distance(0);
Serial.println(test);
  forward();
 delay(2000);
 test=distance(90);
Serial.println(test);
 back();
 delay(2000);
  Stop();
 delay(5000);

why?????????????????????????that's my question
is serial.read effect other functions??
or my Arduinoa is bad and Act foolishly??
and you should known that when i used serial.read all functions didn't work except Step1() ,which do the first step of parking
and really thanks for Your patience with me
A.A.K ;)

Robin2

and really thanks for Your patience with me

Please post the two complete programs from which you have taken the snippets so I can understand the context and the changes.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ahmedashraf30515

#8
Nov 11, 2017, 12:39 pm Last Edit: Nov 13, 2017, 05:29 pm by ahmedashraf30515
that code is for testing that it will work well only later i will modify it that is the change in loop()
Code: [Select]

//motor 1
int en1=6;//speed of motor 1
int M11=7;
int M12=8;
// motor 2
int en2=5;
int M21=2;
int M22=4;
int spee=100;
char g;
void setup() {
  servo.attach(9);
  pinMode(en1,OUTPUT);
pinMode(M11,OUTPUT);
pinMode(M12,OUTPUT);
pinMode(en2,OUTPUT);
pinMode(M21,OUTPUT);
pinMode(M22,OUTPUT);
Serial.begin(9600);
 
}

void loop() {
  forward();
 delay(2000);
 back();
 delay(2000);
  Stop();
 delay(5000);
 
}
A.A.K ;)

ahmedashraf30515

the first code is working ,it is  just testing code i will modify it later after understanding that problem
the seconde is my old code didn't work
A.A.K ;)

Robin2

I have looked at your two programs but I am not sure what it is that is not happening in the second program.

Let's start at the beginning. It looks like the code in loop() in your first program just calls the function distance() a few times with different angles. And after each call there is a long delay().

In the second program loop() seems to expect a value from Serial which it then uses to choose a particular movement. Does any of that work? Comment out all the distance() related code in that version and add a Serial.print() statement so you can see what value is being received with Serial.read()

You should really use if (Serial.available() > 0) { to check if there is a character available before you call Serial,read(). Better still Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Two or three hours spent thinking and reading documentation solves most programming problems.

ahmedashraf30515

#11
Nov 11, 2017, 06:27 pm Last Edit: Nov 11, 2017, 06:35 pm by ahmedashraf30515
thanks i will try  if (Serial.available() > 0) { } maybe it be it's  real problem
but:
The puzzling thing is that step1() function work when i send 6 by serial monitor ,that meaning it is received from the usb but any other character has no effect when i send 1 or 2 0r any other number it never work
anyway i will try it and tell you the result
anther question : what is the best battery to supply the servo s3003?
In the second program loop() seems to expect a value from Serial which it then uses to choose a particular movement. Does any of that work? Comment out all the distance() related code in that version and add a Serial.print() statement so you can see what value is being received with Serial.read()

Serial Input Basics - simple reliable ways to receive data.

...R


i will try this too
A.A.K ;)

Robin2

but any other character has no effect when i send 1 or 2 0r any other number it never work
Without code to let you see what data is actually received you are trying to find a black cat in a dark room.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ahmedashraf30515

Without code to let you see what data is actually received you are trying to find a black cat in a dark room.

...R
thanks Robin the code is working now realiy your advice help me alot and i found the problem at code
my last question : can i power servo s3003 from arduino uno only ???? or i need 4xA battery 1.5v?
A.A.K ;)

Robin2

can i power servo s3003 from arduino uno only ???? or i need 4xA battery 1.5v?
Servos should not draw power through the Arduino.

Make sure the servo GND is connected to the Arduino GND.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up