Pages: [1] 2 3   Go Down
Author Topic: Two Potentiometer Arduino to xbee to xbee to Arduino to two Led's  (Read 1642 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:


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
Code:
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);
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46061
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(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.


  
« Last Edit: July 28, 2013, 08:16:28 pm by BenBenBen » Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8919
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
//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
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46061
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

http://lab.guilhermemartins.net/2008/12/24/serial-comunication-between-arduinos-with-wire-wireless/

One Pot

Code:
// 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

Code:

// 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));

}
}

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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......
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8919
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46061
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)






 
Code:

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);
}
« Last Edit: July 31, 2013, 09:12:38 pm by BenBenBen » Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2184
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8919
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 

Code:
// 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="";
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:

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);
}

Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2184
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

Code:
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);
    }
  }
}
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?]
« Last Edit: August 01, 2013, 10:25:08 pm by Runaway Pancake » Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4019
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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.
Logged

Examples can be found in your IDE.

Pages: [1] 2 3   Go Up
Jump to: