Skip to main content
Arduino

Return to Answer

replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

The problem is the same as last time (XBee+Arduino communication problem XBee+Arduino communication problem)

You are sending the data via:

Serial.println(val1);

which outputs an ASCII representation of the number.

But in your reading, you are reading in a byte value. Actually, I just realised you are reading in a char which is a signed (-128 to 127) value:

char inByte = portOne.read();

first, change that to:

byte inByte = portOne.read();

Now, you have three options:

  1. If (as you say) the numbers you expect are around 200 and will always be below 256 (and positive) then you could send a byte value instead. That way, when you read the byte in, you get the same value. You would do this using Serial.write(val1); instead of print.

  2. If you need to send larger than a byte value, eg int, then send as two bytes, HIGH and LOW. Then on the receiving end, take in two bytes when reading that data.

  3. Send as plain text but read in as plain text. Since you have delimiters, keep reading in the serial until you have all the data you need and then break it down. (You could adapt the code I posted in the answer above to do that).

edit: a couple of other points with your code:

  • You're doing an analogWrite to digital pins 0, 1, and 2. Was that your intention? analogWrite is reserved for sending out PWM signals on only those pins that are capable (see: http://arduino.cc/en/Reference/analogWrite )

  • analogRead will read a value of 0-1023 and if your intention is to just use PWM at the other end, then step it down to a value of 0-255. In that case you can get away with writing byte values to Serial.write.

  • You're never setting the pinMode of the outputs in the receiver.

  • You're sending val1, val2, and val3 out via Serial.println which will send your value out (as ASCII), followed by a newline, so the bytes sent from that one line will be 32 30 30 0D 0A. You have to interpret that at the other end.

  • You're also using software serial on the hardware serial port (you're assigning digital pins 0 and 1. These are the hardware serial pins). BUT you're also initialising it via hardware serial. I'm guessing that it really wouldn't matter (if the pins are aligned), but you can't expect to receive data via software serial and send data via hardware serial if they're all using the same pins.

The problem is the same as last time (XBee+Arduino communication problem)

You are sending the data via:

Serial.println(val1);

which outputs an ASCII representation of the number.

But in your reading, you are reading in a byte value. Actually, I just realised you are reading in a char which is a signed (-128 to 127) value:

char inByte = portOne.read();

first, change that to:

byte inByte = portOne.read();

Now, you have three options:

  1. If (as you say) the numbers you expect are around 200 and will always be below 256 (and positive) then you could send a byte value instead. That way, when you read the byte in, you get the same value. You would do this using Serial.write(val1); instead of print.

  2. If you need to send larger than a byte value, eg int, then send as two bytes, HIGH and LOW. Then on the receiving end, take in two bytes when reading that data.

  3. Send as plain text but read in as plain text. Since you have delimiters, keep reading in the serial until you have all the data you need and then break it down. (You could adapt the code I posted in the answer above to do that).

edit: a couple of other points with your code:

  • You're doing an analogWrite to digital pins 0, 1, and 2. Was that your intention? analogWrite is reserved for sending out PWM signals on only those pins that are capable (see: http://arduino.cc/en/Reference/analogWrite )

  • analogRead will read a value of 0-1023 and if your intention is to just use PWM at the other end, then step it down to a value of 0-255. In that case you can get away with writing byte values to Serial.write.

  • You're never setting the pinMode of the outputs in the receiver.

  • You're sending val1, val2, and val3 out via Serial.println which will send your value out (as ASCII), followed by a newline, so the bytes sent from that one line will be 32 30 30 0D 0A. You have to interpret that at the other end.

  • You're also using software serial on the hardware serial port (you're assigning digital pins 0 and 1. These are the hardware serial pins). BUT you're also initialising it via hardware serial. I'm guessing that it really wouldn't matter (if the pins are aligned), but you can't expect to receive data via software serial and send data via hardware serial if they're all using the same pins.

The problem is the same as last time (XBee+Arduino communication problem)

You are sending the data via:

Serial.println(val1);

which outputs an ASCII representation of the number.

But in your reading, you are reading in a byte value. Actually, I just realised you are reading in a char which is a signed (-128 to 127) value:

char inByte = portOne.read();

first, change that to:

byte inByte = portOne.read();

Now, you have three options:

  1. If (as you say) the numbers you expect are around 200 and will always be below 256 (and positive) then you could send a byte value instead. That way, when you read the byte in, you get the same value. You would do this using Serial.write(val1); instead of print.

  2. If you need to send larger than a byte value, eg int, then send as two bytes, HIGH and LOW. Then on the receiving end, take in two bytes when reading that data.

  3. Send as plain text but read in as plain text. Since you have delimiters, keep reading in the serial until you have all the data you need and then break it down. (You could adapt the code I posted in the answer above to do that).

edit: a couple of other points with your code:

  • You're doing an analogWrite to digital pins 0, 1, and 2. Was that your intention? analogWrite is reserved for sending out PWM signals on only those pins that are capable (see: http://arduino.cc/en/Reference/analogWrite )

  • analogRead will read a value of 0-1023 and if your intention is to just use PWM at the other end, then step it down to a value of 0-255. In that case you can get away with writing byte values to Serial.write.

  • You're never setting the pinMode of the outputs in the receiver.

  • You're sending val1, val2, and val3 out via Serial.println which will send your value out (as ASCII), followed by a newline, so the bytes sent from that one line will be 32 30 30 0D 0A. You have to interpret that at the other end.

  • You're also using software serial on the hardware serial port (you're assigning digital pins 0 and 1. These are the hardware serial pins). BUT you're also initialising it via hardware serial. I'm guessing that it really wouldn't matter (if the pins are aligned), but you can't expect to receive data via software serial and send data via hardware serial if they're all using the same pins.

added about new line and hardware and software serials
Source Link
Madivad
  • 1.4k
  • 8
  • 26

You're doing an analogWrite to digital pins 0, 1, and 2. Was that your intention? analogWrite is reserved for sending out PWM signals on only those pins that are capable (see: http://arduino.cc/en/Reference/analogWrite )

analogRead will read a value of 0-1023 and if your intention is to just use PWM at the other end, then step it down to a value of 0-255. In that case you can get away with writing byte values to Serial.write.

You're never setting the pinMode of the outputs in the receiver.

  • You're doing an analogWrite to digital pins 0, 1, and 2. Was that your intention? analogWrite is reserved for sending out PWM signals on only those pins that are capable (see: http://arduino.cc/en/Reference/analogWrite )

  • analogRead will read a value of 0-1023 and if your intention is to just use PWM at the other end, then step it down to a value of 0-255. In that case you can get away with writing byte values to Serial.write.

  • You're never setting the pinMode of the outputs in the receiver.

  • You're sending val1, val2, and val3 out via Serial.println which will send your value out (as ASCII), followed by a newline, so the bytes sent from that one line will be 32 30 30 0D 0A. You have to interpret that at the other end.

  • You're also using software serial on the hardware serial port (you're assigning digital pins 0 and 1. These are the hardware serial pins). BUT you're also initialising it via hardware serial. I'm guessing that it really wouldn't matter (if the pins are aligned), but you can't expect to receive data via software serial and send data via hardware serial if they're all using the same pins.

You're doing an analogWrite to digital pins 0, 1, and 2. Was that your intention? analogWrite is reserved for sending out PWM signals on only those pins that are capable (see: http://arduino.cc/en/Reference/analogWrite )

analogRead will read a value of 0-1023 and if your intention is to just use PWM at the other end, then step it down to a value of 0-255. In that case you can get away with writing byte values to Serial.write.

You're never setting the pinMode of the outputs in the receiver.

  • You're doing an analogWrite to digital pins 0, 1, and 2. Was that your intention? analogWrite is reserved for sending out PWM signals on only those pins that are capable (see: http://arduino.cc/en/Reference/analogWrite )

  • analogRead will read a value of 0-1023 and if your intention is to just use PWM at the other end, then step it down to a value of 0-255. In that case you can get away with writing byte values to Serial.write.

  • You're never setting the pinMode of the outputs in the receiver.

  • You're sending val1, val2, and val3 out via Serial.println which will send your value out (as ASCII), followed by a newline, so the bytes sent from that one line will be 32 30 30 0D 0A. You have to interpret that at the other end.

  • You're also using software serial on the hardware serial port (you're assigning digital pins 0 and 1. These are the hardware serial pins). BUT you're also initialising it via hardware serial. I'm guessing that it really wouldn't matter (if the pins are aligned), but you can't expect to receive data via software serial and send data via hardware serial if they're all using the same pins.

Looked more deeply at their code.
Source Link
Madivad
  • 1.4k
  • 8
  • 26

The problem is the same as last time (XBee+Arduino communication problem)

You are sending the data via:

Serial.println(val1);

which outputs an ASCII representation of the number.

But in your reading, you are reading in a byte value. Actually, I just realised you are reading in a char which is a signed (-128 to 127) value:

char inByte = portOne.read();

first, change that to:

byte inByte = portOne.read();

Now, you have three options:

  1. If (as you say) the numbers you expect are around 200 and will always be below 256 (and positive) then you could send a byte value instead. That way, when you read the byte in, you get the same value. You would do this using Serial.write(val1); instead of print.

  2. If you need to send larger than a byte value, eg int, then send as two bytes, HIGH and LOW. Then on the receiving end, take in two bytes when reading that data.

  3. Send as plain text but read in as plain text. Since you have delimiters, keep reading in the serial until you have all the data you need and then break it down. (You could adapt the code I posted in the answer above to do that).

edit: a couple of other points with your code:

You're doing an analogWrite to digital pins 0, 1, and 2. Was that your intention? analogWrite is reserved for sending out PWM signals on only those pins that are capable (see: http://arduino.cc/en/Reference/analogWrite )

analogRead will read a value of 0-1023 and if your intention is to just use PWM at the other end, then step it down to a value of 0-255. In that case you can get away with writing byte values to Serial.write.

You're never setting the pinMode of the outputs in the receiver.

The problem is the same as last time (XBee+Arduino communication problem)

You are sending the data via:

Serial.println(val1);

which outputs an ASCII representation of the number.

But in your reading, you are reading in a byte value. Actually, I just realised you are reading in a char which is a signed (-128 to 127) value:

char inByte = portOne.read();

first, change that to:

byte inByte = portOne.read();

Now, you have three options:

  1. If (as you say) the numbers you expect are around 200 and will always be below 256 (and positive) then you could send a byte value instead. That way, when you read the byte in, you get the same value. You would do this using Serial.write(val1); instead of print.

  2. If you need to send larger than a byte value, eg int, then send as two bytes, HIGH and LOW. Then on the receiving end, take in two bytes when reading that data.

  3. Send as plain text but read in as plain text. Since you have delimiters, keep reading in the serial until you have all the data you need and then break it down. (You could adapt the code I posted in the answer above to do that).

The problem is the same as last time (XBee+Arduino communication problem)

You are sending the data via:

Serial.println(val1);

which outputs an ASCII representation of the number.

But in your reading, you are reading in a byte value. Actually, I just realised you are reading in a char which is a signed (-128 to 127) value:

char inByte = portOne.read();

first, change that to:

byte inByte = portOne.read();

Now, you have three options:

  1. If (as you say) the numbers you expect are around 200 and will always be below 256 (and positive) then you could send a byte value instead. That way, when you read the byte in, you get the same value. You would do this using Serial.write(val1); instead of print.

  2. If you need to send larger than a byte value, eg int, then send as two bytes, HIGH and LOW. Then on the receiving end, take in two bytes when reading that data.

  3. Send as plain text but read in as plain text. Since you have delimiters, keep reading in the serial until you have all the data you need and then break it down. (You could adapt the code I posted in the answer above to do that).

edit: a couple of other points with your code:

You're doing an analogWrite to digital pins 0, 1, and 2. Was that your intention? analogWrite is reserved for sending out PWM signals on only those pins that are capable (see: http://arduino.cc/en/Reference/analogWrite )

analogRead will read a value of 0-1023 and if your intention is to just use PWM at the other end, then step it down to a value of 0-255. In that case you can get away with writing byte values to Serial.write.

You're never setting the pinMode of the outputs in the receiver.

removed assumption, it's apparent from the code
Source Link
Madivad
  • 1.4k
  • 8
  • 26
Loading
added 147 characters in body
Source Link
Madivad
  • 1.4k
  • 8
  • 26
Loading
Source Link
Madivad
  • 1.4k
  • 8
  • 26
Loading

AltStyle によって変換されたページ (->オリジナル) /