hi every body , i've been looking for how i can control servos by a wireless way , i recently found a way to do that , i have the codes for two arduinos transmitter and receiver controlling 4 servos , i have received data from the transmitter on the serial monitor of the receiver , but unfortunately with no movement of any servos , in addition that i have changed the location for (servo: refresh ) , but i've got the same result , no movement , can any one till me why is that , what is the problem , how to solve , and i will be so thankful .
here are the codes:
transmitter:
#include <SoftwareServo.h>
#include <VirtualWire.h>
const int numberOfAnalogPins = 4; // how many analog pins to read
int data[numberOfAnalogPins]; // the data buffer
const int dataBytes = numberOfAnalogPins * sizeof(int);
void setup() {
vw_set_ptt_pin(12);
//vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
}
void loop() {
int values = 0;
for(int i=0; i <= numberOfAnalogPins; i++) {
data[i] = analogRead(i); // store the values into the data buffer
}
send((byte*)data, dataBytes);
delay(500); //send every second
}
void send (byte *data, int nbrOfBytes) {
vw_send(data, nbrOfBytes);
vw_wait_tx(); // Wait until the whole message is gone
}
and the receiver :
#include <SoftwareServo.h>
#include <VirtualWire.h>
SoftwareServo myservo1;
SoftwareServo myservo2;
SoftwareServo myservo3;
SoftwareServo myservo4;
const int numberOfAnalogPins = 4; // how many analog integer values to receive
int data[numberOfAnalogPins]; // the data buffer
int value[numberOfAnalogPins];
// the number of bytes in the data buffer
const int dataBytes = numberOfAnalogPins * sizeof(int);
byte msgLength = dataBytes;
int dl=20;
void setup() {
myservo1.attach(9);
myservo2.attach(10);
myservo3.attach(8 );
myservo4.attach(12);
Serial.begin(9600);
Serial.println("Ready");
// Initialize the IO and ISR
//vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_set_rx_pin(11);
vw_rx_start(); // Start the receiver
}
void loop(){
if (vw_get_message((byte*)data, &msgLength)) { // Non-blocking
Serial.println("Got: ");
if(msgLength == dataBytes){
for (int i = 0; i < numberOfAnalogPins; i++) {
Serial.print("pin ");
Serial.print(i);
Serial.print("=");
Serial.println(data[i]);
value[i]=map(data[i],0,1023,0,179); // Write into the servo
}
myservo1.write(value[0]);
delay(dl);
myservo2.write(value[1]);
myservo3.write(value[2]);
SoftwareServo::refresh(); //refresh the servo
myservo4.write(value[3]);
SoftwareServo::refresh(); //refresh the servo
delay(dl);
}
else {
Serial.print("unexpected msg length of ");
Serial.println(msgLength);
}
Serial.println();
}
}
Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long to study quickly without copying to a text editor.
adelphysi:
ok ... but do you have the solution sir.....
Before a solution emerges it is necessary to understand the problem. The code is still almost unreadable because you have not used and indenting. Use the AutoFormat tool and you will find it is much easier to make sense of it.
You have code in your receive program to print the received data - is it showing the correct data?
I have no experience of the SoftwareServo library - is it not possible to use the regular Servo library? Is the SoftwareServo library capable of controlling 4 servos and allowing the Arduino to do other stuff?
Have you written a short program to figure out how to get the SoftwareServo library to work?
If you do need to use the SoftwareServo library I suspect a single call to refresh() will be sufficient if you remove the delay()s from loop()
You may need to remove all the Serial.print() stuff from loop() to get refresh() to happen fast enough - printing is slow.
Your servos should NOT be powered from the Arduino 5v pin - they should have their own power supply with a common GND with the Arduino.
adelphysi:
it is easy for any programer to under stand what the codes mean
I would never claim to be a programmer but I am prepared to dispute that claim. It may be possible to understand your code if enough time is given over to that. But not in the 5 minutes that is all you get for free Help us to help you.
Try the ServoTimer2 library (sorry for not mentioning it in my earlier Post). If it does not conflict with the other libraries you are using it will perform much better than the SoftwareServo library.
of course i have wrote codes for software servo
You won't have to study too many Threads on this Forum to understand why I was unsure about that.
thank you sir , so i will try the library that you mentioned , i don't know if it will give me the right resolution i am looking for but i will rewrite the code by adding this library , hope the project will work.
it is easy for any programer to under stand what the codes mean as they are not very complicated ,
If you believe that, you should have solved the problem already.
I understand "what the codes mean" but don't know why it doesn't work. One of the requirements of the SoftwareServo library is that you call the "refresh" method at least once every 50 milliseconds.
It is impossible to tell from the code whether that condition is met, but I'll bet that it isn't. For one, VirtualWire, or printing, may interrupt the main program exectution for more than 50 milliseconds.
adelphysi:
it is easy for any programer to under stand what the codes mean
A good analogy is the idea that any doctor can figure out what is wrong with a human body. But think how much easier it is for the doctor if the patient gives him information about his problem.
yes sir it is showing the correct data exactly on serial monitor .
servo library conflicts with serial wire and virtual wire so i have used software library .
I (perhaps mistakenly) take that to mean he is seeing the correct data recived via virtual wire AND that the servo (not SoftwareServo) library conflicts with virtual wire.
On that basis I have tentatively concluded either that SoftwareServo is being used incorrectly (or inappropriately) or it is not working for the OP.
It would be nice to get more frequent feedback from the OP.
finally i found the solution ...... the no respond of servos was due to the delay of the transmitted
data , which was 500 when reducing delay value ever motor work with out delay , but there is another problem that when adjusting one of the potentiometer value the main motor most affected but the other motors are little affected too !