Two Potentiometer Arduino to xbee to xbee to Arduino to two Led's

I would like to know how to read the serialin data, so I can make the led's ramp up from 0 to 253.

The data I am reading on the rx side of the serial Monitor is this
(Sensor 1 = 255202255 Sensor 2 = 2550254 Sensor 1 = 255202255 Sensor 2 = 2550254)
As I ramp up the pot, the data moves from 0 to 202 on the Sensor = 1.

Here is the programs I am working with.

TX

int analogValue2, analogValue5, val2, val5;

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
}

void loop()
{
// read analog input
analogValue2 = analogRead(0);
analogValue5 = analogRead(4);

// remap values

val2 = map(analogValue2, 0, 1023, 0, 253);  // 254 and 255 for SYNC
val5 = map(analogValue5, 0, 1023, 0, 253);

Serial.write(254); //SYNC char
Serial.write(val2);

Serial.write(255); //SYNC char
Serial.write(val5);

delay(150);
}

RX

byte incomingByte, sensor1, sensor2;

void setup() {

// start serial port at 19200 bps
Serial.begin(19200);
Serial.println("Ready!");

// led pins

pinMode (10, OUTPUT);
pinMode (11, OUTPUT);

delay(1000);

}

void loop() {
if (Serial.available()) {   // are there any bytes available on the serial port ???

// assign bytes to the var ‘incomingByte’
incomingByte = Serial.read();

Serial.print(int(incomingByte));

// from now on is pretty clear I guess   :)

if ((int(incomingByte) == 254)) {
sensor1 = Serial.read();
Serial.print("  Sensor 1 = ");
Serial.print(int(sensor1));
}

if ((int(incomingByte) == 255)) {
sensor2 = Serial.read();
Serial.print("  Sensor 2 = ");
Serial.print(int(sensor2));
}
}

analogWrite (10, sensor1);
analogWrite (11, sensor2);
}

Serial data is NOT guaranteed to be delivered. What are you going to do when one of the bytes gets lost?

You seem to have a misunderstanding of what data each sensor sends. Each sends ONE byte (not two or three) per iteration of loop.

if (Serial.available()) {   // are there any bytes available on the serial port ???

Serial.available() returns the number of bytes available, not TRUE or FALSE. Do NOT use it like it returned TRUE or FALSE.

What IS your problem?

(What IS your problem) ((You PaulS, all your good at Is Ridiculing people. Do you really know anything....)
Last I looked It's said Programming Questions. If I know it all like you say you do, I wouldn't be asking dead
beats like you.........

I am asking for help.........not people like you, A ass.

I made some servo/pot test code below that might be of interest for the rx side. The value is represented by ascii characters, the addressed servo is identified by a letter, and each data packet is delimited by a comma. On the tx side the packet should be easy to generate with the same properties.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Here is the programs I am working with.

So, they do something. You have not described what they do.

You want them to do something. You have not explained what that is.

Clearly, though, what they do and what you want are not the same things. If they were, you're whiny little rant would have been unnecessary and we wouldn't have had to hear from you.

So, I'll ask again. What is your problem? What do you need help with?

Here is the Link, with code, that I trying to get to work. (One pot to Arduino to xbee to xbee to Arduino to Led) I did get to work.

I just want to see if I can get the (Two pot to Arduino to xbee to xbee to Arduino to two Led's). It is working in the movie, but what is

it missing...

One Pot

// SENDER
int analogValue5, val5;

void setup() {
// Serial port enable
Serial.begin(19200);
}

void loop() {
// read analog pin 5
analogValue5 = analogRead(0);

// remap values from the analogValue5 variable to 0 / 255
val5 = map(analogValue5, 0, 1023, 0, 255);

// send the value to the serial port
Serial.write(val5);

}

One Pot

// RECIEVER
byte incomingByte;

void setup() {
// Serial port enable
Serial.begin(19200);

// declare pin 11 as output, this is the LED
pinMode (11, OUTPUT);
}

void loop() {

// if there is bytes available coming from the serial port
if (Serial.available()) {

// set the values to the ‘incomingByte’ variable
incomingByte = Serial.read();

// write the value to the pin 11
analogWrite(11, int(incomingByte));

}
}

I had 77 people look at my Forum, and only two of them people could kind of help me.

Where can I read about this, sending serial data from one xbee to xbee, or readString of data?

Or maybe you people don't know......

BenBenBen:
I had 77 people look at my Forum, and only two of them people could kind of help me.

Where can I read about this, sending serial data from one xbee to xbee, or readString of data?

Or maybe you people don't know......

What have you done so far besides apparently posting somebody else's code?

Or maybe you people don't know......

I've asked twice now for you to explain what the problem is. Each time, you've chosen, instead, to make it somehow our fault that your code and your expectations don't align.

I'd suggest that you get over it. The code IS working correctly. Whatever it is doing, it is exactly what it is supposed to do. Therefore, the problem is that your expectations are wrong.

But, until you explain what they are, and how what the code actually does does not meet those expectations, all you can do is whine. But, not here.

I think what is the problem is. In this line of code on the rx side the led 10 and Led 11, is turned on by the first byte, and stays on for the 254 and 255. I need to read the sec. byte and then send it to the (analogWrite (10, sensor1); and analogWrite (11, sensor2);
That is what I think is going on. But I don't know how to read the sec. byte, should say, the code.

Is there A way to say in code (sensor1 = Serial.read(); sec.byte only)

if ((int(incomingByte) == 254)) {
sensor1 = Serial.read();
Serial.print("  Sensor 1 = ");
Serial.print(int(sensor1));
}

if ((int(incomingByte) == 255)) {
sensor2 = Serial.read();
Serial.print("  Sensor 2 = ");
Serial.print(int(sensor2));
}
}

analogWrite (10, sensor1);
analogWrite (11, sensor2);
}

BenBenBen:
I had 77 people look at my Forum, and only two of them people could kind of help me.
Or maybe you people don't know......

I don't know, maybe I shouldn't jump in, but how does the receiver "know" whether the data it gets is for 'sensor1' or 'sensor2', or doesn't that matter? A byte is a byte; well, here's a byte, I guess - go for it.
Did you want to have a conversation or did you just want to get this thing done, over with?

Anyway, wouldn't a "header" set things up like?

With GPS, a receiver sketch waits for a particular sentence's header; so cued, it then buffers and can intelligently parse (work with) the data accordingly.

About as simple as it gets for two arduinos with appropriately connected tx/rx/gnd serial connections. Put the below code on both arduinos. Send on and off to the tx arduino via the serial monitor, and the LED on the rx arduino should follow the commands.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    //Serial.println(readString);
    Serial.print(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

So how do I have the rx side of the Arduino read the input of the two pot's. With the code you sent me?
I wish I could look this up and read on how to fix my problem, So I would stop asking on here.

RX

byte incomingByte, sensor1, sensor2;

void setup() {

// start serial port at 19200 bps
Serial.begin(19200);
Serial.println("Ready!");

// led pins

pinMode (10, OUTPUT);
pinMode (11, OUTPUT);

delay(1000);

}

void loop() {
if (Serial.available()) {   // are there any bytes available on the serial port ???

// assign bytes to the var ‘incomingByte’
incomingByte = Serial.read();

Serial.print(int(incomingByte));

// from now on is pretty clear I guess   :)

if ((int(incomingByte) == 254)) {
sensor1 = Serial.read();
Serial.print("  Sensor 1 = ");
Serial.print(int(sensor1));
}

if ((int(incomingByte) == 255)) {
sensor2 = Serial.read();
Serial.print("  Sensor 2 = ");
Serial.print(int(sensor2));
}
}

analogWrite (10, sensor1);
analogWrite (11, sensor2);
}

I guess 254 (oxfe) is your header for sensor1 and 255 (0xff) is for sensor2?
(Reliable?)
If you send 254 (0xfe) followed by sensor1 data then sensor2 data then...

void loop() 
{
  if (Serial.available() > 0) 
  {   
    incomingByte = Serial.read();
    if (incomingByte == 254) 
    {
      sensor1 = Serial.read();
      sensor2 = Serial.read();
      Serial.print("S1 = ");
      Serial.println(sensor1,DEC);
      Serial.print("S2 = ");
      Serial.println(sensor2,DEC);
      analogWrite (10, sensor1);
      analogWrite (11, sensor2);
    }
  }
}

BenBenBen:
I wish I could look this up and read on how to fix my problem, So I would stop asking on here.

There is plenty to read and take advantage of, but what there is doesn't match your needs point-for-point; it's presented as concepts you have to integrate and apply to your situation, as I'm likely doing imperfectly.
[Where's the conversation?]

PaulS:
Serial data is NOT guaranteed to be delivered. What are you going to do when one of the bytes gets lost?

You seem to have a misunderstanding of what data each sensor sends. Each sends ONE byte (not two or three) per iteration of loop.

if (Serial.available()) {   // are there any bytes available on the serial port ???

Serial.available() returns the number of bytes available, not TRUE or FALSE. Do NOT use it like it returned TRUE or FALSE.

What IS your problem?

Gee Paul, calm down. 0 bytes = 0 which also = FALSE while 1 or more is non-zero = TRUE.

That's the test that I use and it has never failed me.

Hi Ben, is Ben just once okay?

I see what you are trying to do and you're not far off the mark. One thing that would help is if you print the incoming bytes as HEX (separated by printed spaces) and put in a newline before printing a SYNCH byte. that way your printed data won't run together.

It would probably not be a bad idea to turn the data into ASCII text or BCD digits that allow more checking than raw binary.

As far as reading incoming data, always check that bytes are available before reading them.

if (Serial.available()) will be true if there is only 1 byte so that test is not good to read 2 bytes.

You're better off checking for and reading 1 byte per pass through loop() anyway, most times there will be no bytes to read, the Arduino could be doing something else like watching for user input or blinking the pin 13 led to show working status.

You should be reading (in HEX) FE data FF data FE data FF data ..... where data is a hex value from 00 to FD. You will need to print leading zeros for data bytes less than 0x10. Do that just to see what you're getting from the transmit.

Set up a state variable with some rules like:

state 0 expects FE. if FE then change state to 1 else change state to 100 (=error)

state 1 expects a data byte in range 0 to FD. if good then process data and change state to 2 else state = error

state 2 expects FF..............

state 100, error, read until you get an FE then set state = 1.

Every time you get a byte and process according to state, return to run loop again. You only process state after reading a byte.

And that is where I set up a BlinkWithoutDelay to serve as a working status indicator.

Ben?
BenBen?
BenBenBen?

Hola.
Que tal?

To all that helped me, I thank you much. I have stopped asking things on here, and I am trying to get the HEX to work. The incoming bytes as HEX that is. The code that Runaway Pancake work's great, but when (one of the bytes gets lost). The led's blink.
I will get it, because I will not stop.......

What if you take out the Serial.print stuff?

    if (incomingByte == 254) 
    {
      sensor1 = Serial.read();
      sensor2 = Serial.read();
      analogWrite (10, sensor1);
      analogWrite (11, sensor2);
    }

Better still, make sure there's something to read, before reading it.