Friday, November 12, 2010

Serial Communications using the TX and RX pins

I have successfully gotten two arduinos to communicate with a very simple program.  I used the push button example, the switch case program, along with serialprint() serialread() examples from the arduino home page... combining these three codes allows me to use a button to print the ascii coded letter m or n onto the serial pins (RX and TX) then the other arduino will read the letter n or m and execute a switch case program to turn on either a red or yellow led.  Of course this is a very simplified example but it demonstrates how I will use an indicator of which state is called upon by the user and serially communicate that data to another arduino which will display that states energy consumption information


here are some pictures showing the last 3 hours of work.... lol


----------------------------------------------------------------------------------
//SERIAL OUT PROGRAM

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
char val = 0;
char val1 = 0;
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {   
    // turn LED on:   
    digitalWrite(ledPin, HIGH);
      val = 'm';
      if (val != val1) {
           Serial.print(val);
           val1=val;
           delay(200);
  }


  }
      else {
        // turn LED off:
        digitalWrite(ledPin, LOW);
          val = 'n';
          if (val != val1) {
               Serial.print(val);
               val1=val;
               delay(200);
  }
  }
}


--------------------------------------------------------------------------------------

//SERIAL IN PROGRAM

int incomingByte = 0;    // for incoming serial data

void setup() {
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
    Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
}

void loop() {

    // send data only when you receive data:
     if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();

        // say what you got:
        Serial.print("I received: ");
        Serial.println(incomingByte,BYTE);
                switch (incomingByte) {
                        case 'm':
                            digitalWrite(4, LOW);   // sets the LED on
                            //delay(4000);                  // waits for a second
                            digitalWrite(5,HIGH);    // sets the LED off
                            //delay(1000);
                            break;
                        case 'n':
                            digitalWrite(5, LOW);   // sets the LED on
                            //delay(4000);                  // waits for a second
                            digitalWrite(4, HIGH);    // sets the LED off
                            //delay(1000);
                            break;
                        default:
                          break;
  }

}
}
these are the two programs I have written from pieces of code I found on the Arduino website

No comments:

Post a Comment