Pages: [1]   Go Down
Author Topic: I need to change code from character to a string (I think)  (Read 481 times)
0 Members and 1 Guest are viewing this topic.
Burlington, Ontario, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I found a processing sketch and an arduino sketch that turned off and on an LED.  I am trying to expand the sketches so that I can turn off and on multiple LEDs.

In the following processing code I tried to change the port.write('L'); port.write('H'); to port.write('H1'); port.write('H2'); and so on but the Arduino sketch did not work.  I then changed the port.write('L'); port.write('H'); to port.write("L1"); port.write("H1"); but I could not figure out how to change the Arduino sketch.

So I guess I'm asking is how can I change the Processing and Arduino Sketches to get the Processing to send out L1 or H1 to turn LED1 on or off,
L2 or H2 to turn LED2 on or OFF.


Code:
//Turn LED on and off if buttons pressed where
  //H = on (high) and L = off (low)
  if(mousePressed) {
    if(rect1.pressed()) {            //OFF button
      currentcolor = rect1.basecolor;
      port.write('L');
    } else if(rect2.pressed()) {    //ON button
      currentcolor = rect2.basecolor;
      port.write('H');
    }
  }
  if(mousePressed) {
    if(rect3.pressed()) {            //OFF button
      currentcolor = rect3.basecolor;
      port.write('L');
    } else if(rect4.pressed()) {    //ON button
      currentcolor = rect4.basecolor;
      port.write('H');
    }
  }
  if(mousePressed) {
    if(rect5.pressed()) {            //OFF button
      currentcolor = rect5.basecolor;
      port.write('L');
    } else if(rect6.pressed()) {    //ON button
      currentcolor = rect6.basecolor;
      port.write('H');
    }
  }
  if(mousePressed) {
    if(rect7.pressed()) {            //OFF button
      currentcolor = rect7.basecolor;
      port.write('L');
    } else if(rect8.pressed()) {    //ON button
      currentcolor = rect8.basecolor;
      port.write('H');
    }
  }

This is the Arduino sketch

Code:
char val; // variable to receive data from the serial port
int ledpin1 = 10; // LED connected to pin 10 (on-board LED)
int ledpin2 = 6; // LED connected to pin 6 (on-board LED)
int ledpin3 = 7; // LED connected to pin 7 (on-board LED)
int ledpin4 = 8; // LED connected to pin 8 (on-board LED)
void setup() {
  pinMode(ledpin1, OUTPUT);  // pin 10 (on-board LED) as OUTPUT
  pinMode(ledpin2, OUTPUT);  // pin 6 (on-board LED) as OUTPUT
  pinMode(ledpin3, OUTPUT);  // pin 7 (on-board LED) as OUTPUT
  pinMode(ledpin4, OUTPUT);  // pin 8 (on-board LED) as OUTPUT
  Serial.begin(115200);       // start serial communication at 115200bps
}
void loop() {
  if( Serial.available() )       // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'
  }
  if( val == 'H' )               // if 'H' was received
  {
    digitalWrite(ledpin1, HIGH);  // turn ON the LED
  }
  if( val == 'L' )               // if 'H' was received
  {
    digitalWrite(ledpin1, LOW);  // turn OFF the LED
  }
  if( val == 'H' )               // if 'H' was received
  {
    digitalWrite(ledpin2, HIGH);  // turn ON the LED
  }
  if( val == 'L' )               // if 'H' was received
  {
    digitalWrite(ledpin2, LOW);  // turn OFF the LED
  }
  if( val == 'H' )               // if 'H' was received
  {
    digitalWrite(ledpin3, HIGH);  // turn ON the LED
  }
  if( val == 'L' )               // if 'H' was received
  {
    digitalWrite(ledpin3, LOW);  // turn OFF the LED
  }
  if( val == 'H' )               // if 'H' was received
  {
    digitalWrite(ledpin4, HIGH);  // turn ON the LED
  }
  if( val == 'L' )               // if 'H' was received
  {
    digitalWrite(ledpin4, LOW);  // turn OFF the LED
  }
  delay(100);                    // wait 100ms for next reading
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13884
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


try to use different chars for different leds ... (simplest solution)
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50319
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I then changed the port.write('L'); port.write('H'); to port.write("L1"); port.write("H1"); but I could not figure out how to change the Arduino sketch.
Not in the code you posted.

Why can't you figure out that you now need to wait for there to be two characters to read, and when there are, read them both? The first defines the state and the second defines which LED the state applies to.
Logged

Pages: [1]   Go Up
Jump to: