Go Down

Topic: Serial Control by a Digital Player (Read 872 times) previous topic - next topic

Markav

Code: [Select]
Hi I'm working on a project to simply switch on and off the digital outputs via serial from a digital player ( Roku Brightsign)

I'm not good with code  - learning but have used this code from this site and successfully got the two devices to talk.

I'm using a Mega 2560 with an RS232 shield attached and want to turn on or off upto 40 outputs and reset them all off via the serial port.

using the code it only seems to work with letters and I'm going to need at least 81 simple commands.

My origonal plan was to send numbers like 1 to 40 for on and 50 to 90 for off and say 99 for reset.

This is the code I have that will switch the internal led on and off great.

[code
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

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

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}
]

PaulRB

#1
Aug 01, 2014, 06:09 pm Last Edit: Aug 01, 2014, 07:05 pm by PaulRB Reason: 1
Hi, your message got a little garbled but can still be understood and at least you tried to use code tags correctly on your first post.

Can you decide what format of message is sent by the Roku? For example you could send "03H" to make output 3 high or "36L" to set output 36 to low, or "00X" for all outputs low? If so, it should be really easy to code for that.

You would change your sketch to check for at least 3 characters waiting to be read, then read 3 characters, turning the first two into a number, then interpreting the third character as you are doing now.

Code: [Select]
int ledPin; // the pin that a LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pins as an output:
  for (ledPin=2; ledPin<=42; ledPin++) {
    pinMode(ledPin, OUTPUT);
  }
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() >= 3) {
    // read the parameter bytes in the serial buffer:
    ledPin = (Serial.read() - '0') * 10;
    ledPin += (Serial.read() - '0');
    // read the command byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
    // if it's an X turn off all LEDs:
    if (incomingByte == 'X') {
      for (ledPin=2; ledPin<=42; ledPin++) {
        digitalWrite(ledPin, LOW);
      }
    }
  }
}


Paul

Markav

Hi Thanks for your reply I will give it a try and also try and understand what you have written on Tuesday morning, as away for my daughters birthday for a couple of days.
I bought a book Getting started with Arduino but it's to simple for this.Will look for something better.

I think the Roku should be able to send those codes , they are what I normaly work with but not connecting them to micros. it should exspand what I can do greatly.
Thanks for your help

Markav

Thank you PaulRB your code worked flawlessly.

Thanks Again
Mark

Go Up