0

I want to send a character from a Zigbee transmitter to an Arduino Mega 2560. After the character is received, Arduino should send some data back to Zigbee. But Arduino is always receiving junk values at the console. If I send a character, suppose 'a', instead of 'a', Arduino console is always showing 'y'. It is the same for every character I send.

#include <EEPROM.h>
#include <SoftwareSerial.h>
SoftwareSerial XBee(19, 18); // RX, TX
int addr = 0;
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds
void setup()
{

  XBee.begin(9600);
  Serial1.begin(9600);
  Serial.begin(9600);
  startMillis = millis();  //initial start time
 }
void loop()
{
  int id = 1;
  EEPROM.write(addr, id);
  if (Serial1.available()>0)
    {          
      currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    char recbyte=Serial.read();
    Serial.print(recbyte);
    // delay(1500);
    Serial1.write(id);
    Serial1.write(" 30 ");
    Serial1.write(" 99 ");
    Serial1.write(" 72 ");
    Serial1.write(" 1098 ");
    Serial1.write(" 728 ");
    Serial1.write(" 578 ");
    // Serial1.write("$");
    Serial1.write('PS=1\r\n');
    Serial1.end();
    startMillis = currentMillis;
  }
    }
}  

migrated from electronics.stackexchange.com Nov 26 '18 at 18:59

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • There is a code formatting button on the editor toolbar but your code indentation is a big mess. If you are that careless then you can expect trouble. Use the edit link under your question to fix it and check the preview before you press "Save Edits". – Transistor Nov 26 '18 at 17:59
  • @Transistor I got it.... For Akhila, have you tried other baud rates? – KingDuken Nov 26 '18 at 18:02
  • @King: I approved your edit but it's still not right. Look at the close braces at the bottom. There are other problems too. The author should fix the question. – Transistor Nov 26 '18 at 18:04
  • 1
    Whatever your actual issue is, having an EEPROM write in the the outermost uncondition part of your loop() is a mistake. – Chris Stratton Nov 26 '18 at 18:10
  • 1
    Your problem however is likely that you seem to be trying to take serial input from the PC. Perhaps you mean to take it from Serial1 to which it seems you have the XBEE connected? – Chris Stratton Nov 26 '18 at 18:12
1

You have created a SoftwareSerial instance called XBee which is taking pins 17 and 18. You then use Serial1 which is pins 17 and 18.

You do not want, nor need, the XBee SoftwareSerial instance, since you are using hardware serial pins. Delete it and all references to it.

Also, you are turning off Serial1 after using it, which means it will never work again:

Serial1.end();

You would only use that instruction in very very specific circumstances. Delete it.

After checking to see if there is data available to read on Serial1 you then proceed to try and read it from Serial:

char recbyte=Serial.read();

That should be:

char recbyte=Serial1.read();

You are writing to EEPROM on every single iteration of loop:

EEPROM.write(addr, id);

It is likely that you have already killed your EEPROM. C'est la vie...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.