Go Down

Topic: serial works via USB but get nothing using max232 connected to pin 0 and 1 (Read 706 times) previous topic - next topic

n8huntsman

I had this sketch working on my mega, using serial1.  I wanted to switch it over to an uno so I changed serial1 to serial.  The sketch works on the UNO when I send the commands through the USB, but when I send it through the max232, I get nothing.  Do I need to disable the USB in the code or something?
Code: [Select]
//macro for calculating the length of arrays
#define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))  
//array called "state"
boolean state [] = {
 LOW, LOW, LOW, LOW };
//array called "command" that defines the commands that need to be received from the remote
int command [] = {
 0x31, 0x32, 0x33, 0x34 };
//get the length of the "command" array and call it "num_commands"
const int NUM_COMMANDS = ARY_LEN(command);
// array called "pins" that defines which pins are outputs
int pins [] = {
 8, 9, 10, 11 };
void setup() {
 Serial.begin(9600);
 //sets pins defined in "pins" array as outputs
 for(int i = 0; i<NUM_COMMANDS; i++) pinMode(pins [i], OUTPUT);
}

void loop() {
 if (Serial.available() > 0) {
   // reads data from Serial and saves it as a variable called "data"
   int data = Serial.read();
   //for-loop that starts with i=0 which is the first box of each array.
   //proceeds through the loop, then adds one to i, for i=1 which is the second
   //box of each array.  does this till i=num_commands at which time it exits the loop
   for(int i = 0; i<NUM_COMMANDS; i++) {
     // if the data received from the serial port = the ith value of the "command" array, then
     if (data == command[i]){
       //change the state of the ith command to its opposite
       state[i] = !state[i];
       //make the ith pin match the state that was set above
       digitalWrite(pins [i], state [i]);
     }
   }
 }
}

Nick Gammon

You can't disable the USB. There are resistors between it and and Tx/Rx pins. Maybe the MAX232 isn't putting out enough power to overcome them.
http://www.gammon.com.au/electronics

n8huntsman

Now that I try it on the Mega.  I can not get serial to work on there either.  Only serial1.  This is the board I'm using for the max232; http://www.emartee.com/product/41624/Arduino%20MAX232%20Serial%20TTL%20To%20232%20Shield
Are there workarounds for this, or should I try to use software serial on some of the other pins?
Since this now appears to be a hardware issue, feel free to move it. Thanks

Nick Gammon

You could try software serial. That avoids fighting with the USB chip.
http://www.gammon.com.au/electronics

n8huntsman

First time using softwareserial.  Is this correct, because it still does not work...
Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);

//macro for calculating the length of arrays
#define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))  
//array called "state"
boolean state [] = {
 LOW, LOW, LOW, LOW };
//array called "command" that defines the commands that need to be received from the remote
int command [] = {
 0x31, 0x32, 0x33, 0x34 };
//get the length of the "command" array and call it "num_commands"
const int NUM_COMMANDS = ARY_LEN(command);
// array called "pins" that defines which pins are outputs
int pins [] = {
 8, 9, 10, 11 };
void setup() {
 mySerial.begin(9600);
 //sets pins defined in "pins" array as outputs
 for(int i = 0; i<NUM_COMMANDS; i++) pinMode(pins [i], OUTPUT);
}

void loop() {
 if (mySerial.available() > 0) {
   // reads data from mySerial and saves it as a variable called "data"
   int data = mySerial.read();
   //for-loop that starts with i=0 which is the first box of each array.
   //proceeds through the loop, then adds one to i, for i=1 which is the second
   //box of each array.  does this till i=num_commands at which time it exits the loop
   for(int i = 0; i<NUM_COMMANDS; i++) {
     // if the data received from the mySerial port = the ith value of the "command" array, then
     if (data == command[i]){
       //change the state of the ith command to its opposite
       state[i] = !state[i];
       //make the ith pin match the state that was set above
       digitalWrite(pins [i], state [i]);
     }
   }
 }
}


edit: Got it.  On the mega, "only the following can be used for RX: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69 "  So the above worked, I just had to change the SoftwareSerial pins.

Go Up