Hey guys im making a rc boat and i have both the codes for the transmitter and recievr working except the servo doesnt move to the position i want. I had designed the code in mind with the Servo.h library but since i am also using the RadioHead library there was a clash between both the libraries using the Timer1 so i used the ServoTimer2 library and now i am able to recieve the data i want from the transmitter but i cant move the servo the way i want. I tried giving it fixed positions like 90degrees and 180 but it still has the same position.I thought t was a problem with my servo and tried the Servo.h library example code sweep and it worked.So i dont know how to use the ServoTimer2 library. I am using 2 arduino uno's.
Transmitter code(it works but just in case):
//Define the input pins for the joystick and the variables to keep track of its state
#include<RH_ASK.h>
#include<SPI.h>
RH_ASK driver;
int Vala = 0;
int Valb = 0;
int potpin = 0;
const int buttonPinf = 7;
const int buttonPinb = 6;
int Valc = 0;
int buttonStatef = 0;
int buttonStateb = 0;
const int transmit_pin = 12;
const int receive_pin = 11;
void setup()
{
Serial.begin(9600);
if (!driver.init())
Serial.println("init failed");
pinMode(buttonPinf, INPUT);
pinMode(buttonPinb, INPUT);
}
void loop()
{ //This will happen over and over and over and over again......
Valb = analogRead(potpin);
Valb = map(Valb, 0, 1023, 0, 180);
buttonStatef = digitalRead(buttonPinf);
buttonStateb = digitalRead(buttonPinb);
if(buttonStatef == HIGH && buttonStateb == HIGH)
{
Vala = 1000;
}
else if( buttonStatef == HIGH)
{
Vala = 2000;
}
else if ( buttonStateb == HIGH)
{
Vala = 3000;
}
else { Vala = 1000;}
{
Valc = Vala + Valb;
driver.send((uint8_t *)&Valc, (uint8_t)2);
driver.waitPacketSent();
Serial.println(Valc);
}
}
Reciever code:
#include<RH_ASK.h>
#include<SPI.h>
RH_ASK driver;
#include<ServoTimer2.h>
ServoTimer2 myservo;
int val = 0;
int servoval = 0;
uint8_t nbBytesReceived=2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (!driver.init())
Serial.println("init failed");
myservo.attach(9);
myservo.write(90);
}
void loop() {
// put your main code here, to run repeatedly:
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
val = int(buf);
if (driver.recv((uint8_t *) (&val), &nbBytesReceived)) {
if (val >= 1000 && val < 4000)
{
if (val >= 1000 && val < 2000)
{
servoval = val - 1000;
}
else if (val >= 2000 && val < 3000)
{
pinMode(7, HIGH);
servoval = val - 2000;
}
else if (val >= 3000 && val < 4000)
{
pinMode(6, HIGH);
servoval = val - 3000;
}
myservo.write(servoval);
Serial.println(val);
}
}
}
Need to do it fast for my school science fair.Thnx in advance
#include <ServoTimer2.h>
ServoTimer2 myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
// goes from 180 degrees to 0 degrees
myservo.write(90); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
}
The servo is supposed to move to 90degrees but suprisingly it movend nowhere near 90degrees. It was more like 160degrees and this was the same result i got when i tried my reciever code. And i changed HIGH to OUTPUT to control the l293d.Thnx for pointing that out.What am i doing wrong?Thnx in advance
The ServoTimer2 library has different position(degrees) values. It didnt work with the regular values that you usually use with the Servo library,i.e, 180, 90, etc. When i randomly typed the servo position to 1500 it moved to 90 degrees so 180 degrees should be 3000. So i changed the values in the map function in arduino from: Valb = map(Valb, 0, 1023, 0, 180); to: Valb = map(Valb, 0, 1023, 0, 3000); and it worked.
The ServoTimer2 library does not use degrees. The parameter for the write() function is a microsecond value between 1000 and 2000 which correspond to full movement one way and the other of the servo. This is equivalent to the writeMicroseconds() function of the standard Servo library.