Hi there!
I am working ona quad copter..
In which I want to control 4 motors by 2 pots.
My problem is I can send and control 2 motors by a pot..
But how do I control other 2?
Wont the serial data of the 2 pots mix up?
I am attaching a sample of my code to montor the inputs and control 2 motors by a pot..
So, the program looks for serial data. There is none there. One nanosecond later, a byte arrives. Why not look at it right away? The Arduino works just as hard "doing nothing" (the delay() call) as it does spinning through the while loop.
Calling loop() from inside setup is not a good idea. After loop() runs once, wait 1 second, then run it again and again in an endless loop. Again, what purpose does that delay() serve?
You need to restructure your code so that it does nothing until serial data is available AND the serial data IS 'M'.
Why do you want to have to go look up what letter to type to start the Arduino going, if you put this away for a while? Compare to 'M' instead of 115, and that won't be necessary.
Wont the serial data of the 2 pots mix up?
Other than the 'M' to start loop going, there is nothing in this code that reads any serial data, so, no there is no chance of mixing up data.
This is my code now,
the character is 's' I wanted a safety feature that my quadcopter will start only when I command it to.
I wanted a time delay of 1 sec to clear the launch pad and all...
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos = 0;
int potv = 2;
int poth = 3;
void setup()
{
Serial.begin(9600);
myservo1.attach(3, 1700, 2400);
myservo2.attach(5, 1700, 2400);
while(Serial.available() <= 0) delay(1000);
int incomingByte = Serial.read();
if(incomingByte == 115) {
loop();
}
}
void loop()
{
int hori=analogRead(poth);
int pos = map(hori,0,1023,0,180);
myservo1.write(pos);
Serial.println(pos)
int vert=analogRead(potv);
int pos1 = map(vert,0,1023,0,180);
myservo2.write(pos1);
Serial.println(pos1)
delay(1000);
}
Now I get 2 values when I open the serial monitor.
But I cant control 2 servos.
I wanna control 2 servos (4 Brushless motors) INDEPENDENTLY with 2 different pots..
It ain't happening
I read about the software serial but couldn't get it
Each time that the while loop executes, it twiddles its thumbs for an entire second, if there is no serial data.
Get rid of the delay() call. Just leave the ; there. The code after the while loop will not execute until there is serial data.
Don't call loop() from setup.
All you need is:
while(Serial.read() != 's');
Yes, we preach that it is necessary to call Serial.available() before calling Serial.read(), but that is a generality, not an absolute necessity.
If there is no serial data available, Serial.read() returns -1, which is certainly not equal to 's'. When there is serial data, it may or may not equal 's'. If not, keep reading/waiting. If it is, the while loop ends, and setup() does, too. The loop() function will then be called immediately.
Now I get 2 values when I open the serial monitor.
But I cant control 2 servos.
Then, you have a hardware problem. There is nothing wrong with the code in loop().
Try swapping the pins that the servos are attached to.
Actually Its gonna be a wireless connection by xbee's between 2 ardu's
And that servo is actually a brushless motor..(1servo = 2brushless motors)
Okay I ll get rid of the delay..
As it's gonna be a quadcopter it has to be wireless. I just wanna control 2 servos wirelessly and independently
by 2 arduinos and 2 xbees 2 pots
Can you suggest code for the same?
Communicating using XBees is exactly the same as communicating using wires. Serial.write(), Serial.print(), and/or Serial.println() to send data. Serial.read() to receive it.
If you are having trouble with controlling two servos with two pots on the same Arduino, it won't help to go wireless at this point.
I didn't tested it but its logical when the same serial connection is gonna send data for both the servo's then both of them will accept the same signal intended for a particular servo.
So, you are having trouble seeing how to send your two potentiometer readings over a serial link to another arduino because you can't tell which is which.
If this is the issue, you just need to devise a communications protocol. Search the forums and you'll find plenty of references. An incredibly basic one would be to send an 'H' before the reading for the Horizontal pot and a 'V' for the vertical one, with an 'X' to mark end of packet to deal with the fact that the servo numbers vary in length (1 to 3 chars). The receiving arduino can then parse the resulting string and apply the data appropriately.
Think about what other commands you might want to send too, it may influence your protocol design. Also consider whether you are concerned about your transmissions being corrupted and whether therefore, you will need some error checking too.
You'll need to describe (summarize) what you still need help with. You've been given plenty of suggestions. Surely you've at least looked at them. What was wrong with the ideas proposed?
Do you really expect us to do your project for you? Without at least a donut?
Ha ha If you do that I ll surely parcel some donuts ..
NO I dont expect that at all..
I found many examples...but I just cant modify it for my project
including this one take a look...
Or just at least give me a link to any topic, according to you which will put me on right track..
okay as you said I put the logic as
say for horizontal pot data it will be
H155E
where h to identify which servo to control and E to denote end of the packet and 155 as the pot reading/value
same for vertical pot
V140E
where v to identify which servo to control and E to denote end of the packet and 140 as the pot reading/value
Okay I will manage the code to send such data bu how do I decode and send these values to the corresponding servos.
really thanks for the help
If the data is collected in a character array, and always contains 5 characters, the array containing the sample would look like this:
char data[6] = {'H', '1', '5', '5', 'E', '\0'};
If you examine the character in position 0, you will know which servo to affect. Then, replace the character in position 0 with a space. Now the array looks like:
char data[6] = {' ', '1', '5', '5', 'E', '\0'};
The value in position 4 will define whether or not the complete packet has arrived, and may or may not actually have been stored. If it is, then you need to change it to a NULL. Now the array looks like:
char data[6] = {' ', '1', '5', '5', '\0', '\0'};
If you pass this array to atoi(), the function will return an integer, 155.