That should be very straight forward - especially with a Micro as pins 0 and 1 are available as Serial1 for communication.
The only cautionary note - since you say you want to work with an existing RS232 system - is that standard RS232 uses 12v wheras the Arduino can only use 5v. You can get chips such as the Max232 that are designed to change the signal levels.
The examples in serial input basics illustrate how to receive data reliably. As the examples are written they use Serial (for communication with a PC) but they can easily be changed to use Serial1 to communicate with another Arduino.
The system this project will integrate into uses rs232 multiplexed through a multmode fiber, so distance is not an issue. It has multiple rs232 comms channels and has 1 available for 5vdc signal levels.
Cheers for the links i will have a thorough read through now.
So i quickly tried two boards with these codes... but I could not get it to work.
I used a serial to usb converter to confirm i was receiving data out on digital pin 1 on the transmitting board. It showed 0 on no button press and 1 on button press, as expected.
I then used the same serial converter to input 1 on to the rx pin (digital input 0) through hyper terminal.. and the led came on when 1 was pressed and was off when 0 was pressed.. as expected.
Though, when connecting tx from the transmitting board to rx on the receiving... button presses produced no effect...
I am sure i have probably missed something simple:)
any ideas?
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
This example code is in the public domain.
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(2000); // delay in between reads for stability
}
/*
Simple LED sketch
*/
int led = 13; // Pin 13
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
// Start up serial connection
Serial.begin(9600); // baud rate
Serial.flush();
}
void loop()
{
String input = "";
// Read any serial input
while (Serial.available() > 0)
{
input += (char) Serial.read(); // Read in one char at a time
delay(5); // Delay for 5 ms so the next char has time to be received
}
if (input == "1")
{
digitalWrite(led, HIGH); // 1
}
else if (input == "0")
{
digitalWrite(led, LOW); // 0
}
}
I think the input string was picking up extra characters, so your strict comparisons with "1" and "0" were failing.
On reflection, it might have been enough to change this in your sending program, so that CR and LF characters were not sent:
Serial.print(buttonState); // not println
If your requirement is not safety / mission critical, and your comms links are clean, you can probably get away with this simple "single character message" approach.
But if you need to send more data items and/or confirm receipt of the commands, follow the advice above from @robtillaart and @Robin2.