drive by wire code

Hello…

i wrote this code:

#include <Servo.h>

Servo myservo;
int potpin = 4;
int val ;
void setup(){
Serial.begin(9600);
myservo.attach(9);
//1023 cz 10 bits
}

void loop(){
val = analogRead(potpin);
val = map(val,0,1023,5,175);
myservo.write(val);
Serial.println(val);
delay(30);

}

to control servo using arduino and a potentiometer

and wanted to add the feedback effect so i extended the code to :

#include <Servo.h>
#include <stdio.h>

Servo myservo;//servo on the wheels
Servo urservo;//servo on the steering wheel
int potpin = 3; //pot of the steering wheel
int potpin2 = 4; //pot of the feedback
int valOfPot1 ;
int valOfPot11 ;
int valOfPot2 ;

void setup(){
Serial.begin(9600); //for serial monitor

myservo.attach(9); //attach the servo digital
urservo.attach(10); //attach servo feedback to pin 10 digital
rotate();//goto rotate
}

int main(){

while(1)
if( analogRead(potpin) != valOfPot1) rotate();//keep on checking, if the value of pot1 changed goto rotate
return(0);
}

void rotate(){

valOfPot1 = analogRead(potpin); //read value from pot1(steering wheel)
valOfPot11 = map(valOfPot1,0,1023,5,175);
Serial.println(valOfPot11); //write on monitor
myservo.write(valOfPot11); //write to servo
delay(30); //delay of the servo
}

void feedback(){
if( analogRead(potpin2) != analogRead(potpin)){
urservo.write( map( analogRead(potpin2) , 0 , 1023 , 5 , 175));
myservo.write( map( analogRead(potpin) , 0 , 1023 , 5 , 175));
main();
}// if the values of the two potentiometers are different, write the value of the 2nd pot to the servos
}

this code has no error but it does not work… Can anybody help me please :D:D

You've redefined main() - this clobbers the built in main which calls setup() and loop().

You have no loop() function

You don't call feedback() at all.

we wrote this:

#include <Servo.h>
#include <stdio.h>

Servo myservo;
Servo urservo;
int potpin = 3; //pot l direction
int potpin2 = 4; //pot l feedback
int ppin = 1;//val of pot1 lal attach
int valOfPot1 ;
int valOfPot11 ;
int valOfPot2 ;

void setup(){
Serial.begin(9600); //lal serial monitor

myservo.attach(9); //attach the servo digital
urservo.attach(10); //attach servo feedback to pin 10 digital mawsul 3al direction
rotate();}

void loop(){

if( analogRead(potpin) != valOfPot1) rotate();
}

void rotate(){

valOfPot1 = analogRead(potpin); //read value from pot direction
valOfPot11 = map(valOfPot1,0,1023,5,175); //map it(mn l 5 lal 175 ) mchen ma yrj l servo 1023 cz 10 bits
Serial.println(valOfPot11); //write on monitor
myservo.write(valOfPot11); //write to servo
delay(30); //mchen tktkub 3al servo (20 la 40)
}

void feedback(){
if( analogRead(potpin2) != analogRead(potpin)){
urservo.write( map( analogRead(potpin2) , 0 , 1023 , 5 , 175));
myservo.write( map( analogRead(potpin) , 0 , 1023 , 5 , 175));

}
}

int main(){
return 0;
}

1- we didn’t call feedback because it will run after rotate();
2- we didn’t knew that we should not write a main;
so we edited this code(as shown above)… but still not working on the hardware.

Thanks for your help :smiley:

Hi, Sorry but feedback will not run after rotate. They are both functions, you call them from the void loop(). When your sketch runs it starts on the first line, runs though to setup, through setup once, then runs continuously around in loop. (Hence its name loop) Can you please put your sketch between code tags, they are the # in the menu above the editing window. Also a CAD or picture of a hand drawn circuit diagram please, I hope you are not going to power the servo from the arduino, servos pull more current than the arduino can supply.

Tom....... :)

void loop(){



if( analogRead(potpin) != valOfPot1) rotate();
}

The only argument for putting the { on the same line as the function/statement that holds water is that it reduces the number of lines that the code takes to display.

Following the compact line with a shit-load of white space makes NO sense.

Put the { on new lines. Get rid of the useless white space.

you call them from the void loop().

I cringe every time someone refers to a function as a void, and I am sure that I not the only one. loop() is a function. It returns no value, hence it is void

Would you refer to this function

int squareAnInt(int a)
{
  return a * a;
}

as "int squareAnInt" ?

Would you refer to this function...as "int squareAnInt" ?

Of course not. It's "the int squareAnInt". Don't you know anything. 8)

Of course. Silly me. :)

In Reply #1 you were told to get rid of main()

In Reply #2 you still have it ???

...R