SwitchesProcessing \ Learning \ Wiring ALPHA 1.0
Author: Hernando Barragán
Description: Wiring is an electronic sketchbook and hardware electronics for developing ideas. It is a context for learning fundamentals of computer programming and prototyping with electronics within the context of the electronic arts.
Keywords: Wiring, Processing, Interactive Media, Electronic Arts, Programming, C, C++, Hernando Barragán

Search wiring.org.co:

This example is for Wiring version 0023+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know.

Switch Colors by CAMILO VILLEGAS
Demonstrates how to read multiple input from five switches and send data using the serial port to a processing application. The processing application change the color of a rectangle according to the data sent.

    int sent = 0;
    int ledpin = 48;     // LED connected to pin 48 (on-board LED)
    int switchpin_a = 0; // switch connected to pin 0
    int switchpin_b = 1; // switch connected to pin 1
    int switchpin_c = 2; // switch connected to pin 2
    int switchpin_d = 3; // switch connected to pin 3
    int switchpin_e = 4; // switch connected to pin 4

    void setup()
    {
      pinMode(ledpin, OUTPUT); // pin 48 (on-board LED) as OUTPUT
      pinMode(switchpin_a, INPUT); // pin 0 as INPUT
      pinMode(switchpin_b, INPUT); // pin 1 as INPUT
      pinMode(switchpin_c, INPUT); // pin 2 as INPUT
      pinMode(switchpin_d, INPUT); // pin 3 as INPUT
      pinMode(switchpin_e, INPUT); // pin 4 as INPUT
      Serial.begin(9600); // start serial communication at 9600bps
      digitalWrite(ledpin, HIGH); // turn ON the Wiring board LED
    }

    void loop()
    {
      if(digitalRead(switchpin_a) == HIGH) // if switch connected to pin 0 is ON
      {
        Serial.print('1'); 	// send 1 to Processing
        sent = 0;
      }
      else if(digitalRead(switchpin_b) == HIGH) // if switch connected to pin 1 is ON
      {
        Serial.print('2'); 	// send 2 to Processing
        sent = 0;
      }
      else if(digitalRead(switchpin_c) == HIGH) // if switch connected to pin 2 is ON
      {
        Serial.print('3'); 	// send 3 to Processing
        sent = 0;
      }
      else if(digitalRead(switchpin_d) == HIGH) // if switch connected to pin 3 is ON
      {
        Serial.print('4'); 	// send 4 to Processing
        sent = 0;
      }
      else if(digitalRead(switchpin_e) == HIGH) // if switch connected to pin 4 is ON
      {
        Serial.print('5'); 	// send 5 to Processing
        sent = 0;
      }
      else {
        if (sent == 0) {
          Serial.print('0'); // send 0 to Processing
          sent = 1;
        }
      }
      delay(100); 	// wait 100ms for next reading
    }

    /* Processing code for this example

    // Switch Colors
    // by CAMILO VILLEGAS

    // depending on the data received from Wiring through the serial port it
    // changes the fill color of a rectangle

    // Created 1 November 2005

    import processing.serial.*;

    Serial port; // create serial port object
    int val; // variable to receive data from the serial port

    void setup()
    {
      size(200, 200);
      noStroke();

      // List all the available serial ports in the output pane.
      // You will need to choose the port that the Wiring board is
      // connected to from this list. The first port in the list is
      // port #0 and the third port in the list is port #2.
      println(Serial.list());

      // Open the port that the Wiring board is connected to (in this case #2)
      // Make sure to open the port at the same speed Wiring is using (9600bps)
      port = new Serial(this, Serial.list()[2], 9600);
    }

    void draw()
    {
      if( 0 < port.available() ) // if data is available to read
      {
        val = port.read(); // read it and store it in 'val'
      }

      background(0); // clear background

      if( val == '1' ) // if 1 was received
      {
        fill(100); // set fill color
      }
      else if( val == '2' ) // if 2 was received
      {
        fill(0); // set fill color
      }
      else if( val == '3' ) // if 3 was received
      {
        fill(0,100,0); // set fill color
      }
      else if( val == '4' ) // if 4 was received
      {
        fill(0,0,100); // set fill color
      }
      else if( val == '5' ) // if 5 was received
      {
        fill(100,0,0); // set fill color
      }
      else { // if other data was received (in this case 0)
        fill(255); // set fill to a different color
      }

      rect(50, 50, 100, 100); // draw rect
    }

    */