Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Code not working on MEGA2560 but works on UNO on: January 11, 2013, 03:29:03 pm
Thanks for all the replies.  Turns out it was a simple error.  I was using pins on the MEGA that didn't support software serial.  I changed the code to use pins that are compatible and the all is well.  Thanks for the help. 


-Mike
2  Using Arduino / Programming Questions / Code not working on MEGA2560 but works on UNO on: January 10, 2013, 09:41:40 pm
Hello all.  I have a program that works correctly on my Arduino UNO but when I upload the program to my MEGA2560 it does not function properly. 

Here is my code....
Code:
#include <SimpleTimer.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
int DIR = 7;
word incoming=0;
SimpleTimer timer;

void setup()
{
  digitalWrite(DIR, LOW);
  pinMode(DIR, OUTPUT);
  Serial.begin(9600);             //PC Serial comm   
  mySerial.begin(9600);          //485 serial communication
  timer.setInterval(500, Poll);  // timed actions setup

}


void loop()
{
  timer.run();              //starts poll function
}

//*******************************************FUNCTIONS***************************************

void Poll()       // function to be called repeatedly
{
  incoming = 0;
  digitalWrite(DIR, HIGH);    //to talk to device
  mySerial.write((byte)0xAA);   
  mySerial.write((byte)0x02);
  mySerial.write((byte)0x00);
  mySerial.write((byte)0x00);
  mySerial.write((byte)0x5E);
  mySerial.write((byte)0xEA);
  Serial.println("Polling Command Sent");

  digitalWrite(DIR, LOW);  //to read form device
  Serial.print("Response: ");
  while(mySerial.available() >= 7)
  {
    incoming = mySerial.read();
    Serial.print(incoming, HEX);
    Serial.print(" ");
  }

  Serial.println();
  Serial.print(incoming); //prints value of incoming for debug
  Serial.println();
 
  switch (incoming)
  {
  case 249:
    Serial.println("Good Response From Poll");
    break;

  case 20:
    Serial.println("SW1 Pressed");
    break;

  case 38:
    Serial.println("SW2 Pressed");
    break;

  }
}



My circuit uses a MAX485 to communicate with a sensor board via 485.  RE and DE are tied together and connected to PIN 7 to send and receive commands.  When using the UNO the sensor board is sent the Polling Command and a good response is received.  If one of the switches is activated then the command is received correctly by the UNO.  When using the MEGA the program uploads to the board but there is no response from the sensor board when the polling command is sent, I know the board is getting the command and sending the response (there are LEDs on the sensor board to indicate a 485 connection) but it is not received by the MEGA.  I get no response, incoming has a value of 0 the correct response is 7 bytes of hex data.  I am using the same circuit and have tried all the serial ports on the MEGA as well as using software serial.  When the same circuit is connected back to the UNO then the commands are sent and received correctly. 

I need to use the MEGA because of all of the I/O for other parts of the project.  I figure I am missing something simple but I'm lost, any help would be greatly appreciated. 


-Mike
Pages: [1]