Hi, I'm reading data from serial port. The data is written there by a 3rdParty software. The data is 9 values in series as bytes (from 0x00 to 0xFF).
Now I should control a servo angle from 0 to 180 according to the value read from COM port.
I can use a slider (0...255) in that 3rdParty application to choose the value for a byte and at the moment I run directly servo with the value read from serial. Now the system works so that the values in the 3rdParty application c. 0...18 are dead and c. 168...255 are also dead, and the servo angle sweeps from 0 to 180 at 3rdParty application values 19...168.
What should I do? I guess I should do some hex to dec change and re map the values to new scale. But whatever I do it doesn't work at all anymore.
Thanks in advance!
This is my code at the moment:
const int ledPin = 13; // the pin that the LED is attached to
#include <Servo.h>
Servo myservo;
void setup()
{
myservo.attach(9);
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
myservo.write(90);
//R1R2R3G1G2G3B1B2B3
int RR1;
int RR2;
int RR3;
int GG1;
int GG2;
int GG3;
int BB1;
int BB2;
int BB3;
while(true){
if (Serial.available() > 8) {
RR1 = Serial.read();
RR2 = Serial.read();
RR3 = Serial.read();
GG1 = Serial.read();
GG2 = Serial.read();
GG3 = Serial.read();
BB1 = Serial.read();
BB2 = Serial.read();
BB3 = Serial.read();
}
digitalWrite(13, HIGH);
//RR1 = map(RR1, 0x00, 0xFF, 0, 180);
myservo.write(RR1);
delay(0);
}
}
Thanks Graynomad, but that's not the solution. As you might notice in my code I've already tried to use map() function. I also tried it exactly as you said. This doesn't work at all so I believe the read value is somehow in wrong type.
And I think I don't get in Arduino values 19?168. Instead I get servo in zero angle when the slider is in 19, and servo angle 180 when slider is in 168.
And how I could debug the values I receive thru COM port on Arduino. I can't use Arduino's COM monitor coz I have this another application already running in COM3.
Thanks for the replies.
The data I read in to Arduino seems to be as it should. I hooked up a LCD display to debug the values, and for my suprise I get values from 0 to 255. The servo just sweeps 0...180 with values 19...168, which is weird.
Anyway thanks for the support so far. Struggle goes on...
Have you tried debugging your sketch using the serial monitor and sending byte values you can type into the serial monitor (space=32 thru -=126)? Also, do you have more details on your third party slider application? If it sends a value at each change in slider position, you could be somewhat flooding the arduino buffer when you move the slider to different positions. There is a free application on the net called portmon that can display what is going on with the pc com port when the slider application is being used.