I am sending data from an XBEE series 1 mounted on an UNO R3 to another XBEE series 1 mounted on another UNO R3. The communication is working; I have checked the readings by plugging the receiving XBEE to my computer and reading it on X-CTU and the reading are correct. On the receiving XBEE/Arduino there is a servo motor which moves depending on the data received.
What I think seems to be happing is that the servo moves twice on readings with more than 2 digits, it moves once for the 1st digit and a second time for the second digit. I am using two different types of sensors and I seem to get the same results which is the servo making two movements not one. The reason I think it is doing this is because I have two similar sketches; one is sending wind speeds from an anemometer to a Servo and the other which is posted below send the Electret Mic readings. To test this theory I have wrote a sketch to send 3 different readings every 10 seconds and each time a reading was received the servo moved twice within the 10 second gap, 1.34, 0.50,0.66.
Any ideas on what is going on with the sketch?
Electret Mic Sketch:
#define VERSION "1.00a0"
const int sampleWindow =250; // Sample window width in mS (500 mS = 20Hz)
unsigned int sample;
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(0);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
Serial.println(volts);
delay(10);
}
Servo Code:
#define VERSION "1.00a0"
#include <Servo.h>
Servo myservo;
int delayTimeF = 17;
int delayTimeM = 25;
int delayTimeS = 30;
int delayTimeVS = 40;
int i = 0; // variable to store the servo position
void setup()
{
Serial.begin (9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void swing(int low, int high) {
for (i=low; i<=high; i++) {
myservo.write(i);
delay(delayTimeVS);
}
for (i=high; i>=low; i--) {
myservo.write(i);
delay(delayTimeVS);
}
}
void loop()
{
if (Serial.available() >0){
//if((Serial.read() >= 0.15) && (Serial.read() < 0.30)){
//swing(100,110);
//} // end of loop
if((Serial.read() >= 0.31) && (Serial.read() < 0.50)){
swing(100,110);
} // end of loop
if((Serial.read() >= 0.51) && (Serial.read() < 0.70)){
swing(100,120);
}
if((Serial.read() >= 0.71) && (Serial.read() < 0.90)){
swing(100,130);
}
if((Serial.read() >= 0.91) && (Serial.read() < 1.50)){
swing(100,140);
}
if((Serial.read() >= 1.51) && (Serial.read() < 2.00)){
swing(100,150);
}
if((Serial.read() >= 2.01) && (Serial.read() < 3.00)){
swing(100,160);
}
if((Serial.read() >= 3.01) && (Serial.read() < 4.00)){
swing(100,170);
}
}
}
A sample reading:
0.50
0.66
0.34
1.34