• Site: Main page | Recipes | Workshops |
  • tinkertoy


    The Wiring code in this recipe is really straightforward.
    Wiring waits for a command to come from the serial port. When it gets a command (1, 2, 3 or 4) it “presses” the relevant button on the remote control.
    Note that when we pass a command to move sideways, the Wiring code “presses” two buttons at once: the left or right button to turn the wheels and the forward button to move (just like the gas pedal in a real car).

    Wiring concepts in this recipe:
    Activating digital pins from Flash

    serial communication (input)

    BEFORE YOU START, connect the USB cable to the Wiring board and your computer.

    Working with the Wiring code of this recipe
    Bridge page ‘For more detailed instructions, see the “Export Wiring Code Guide”‘
    Click inside the text box bellow, select the entire code content (use the ‘Select All’ command) and copy
    Open the Wiring environment and create a new “Sketch” (File > New)

    Paste the code into the new Sketch
    Verify the code (Sketch > Verify)
    Reset the Wiring board
    Export the code to the Wiring board (File > Export to Board)
    Wait for the “Success” message; then reset the Wiring board again

    If you get an error, make sure that the XML broker is NOT running.

    Bridge page: If after quitting the Broker you still are not able to export the code, see the “Wiring Errors Guide”

    //
    //
    //
    // TinkerToy
    // -------------------------------------------------------------------------------
    // InstantSOUP 2004
    // http://www.nastypixel.com/instantsoup
    // ------------------------------------------------------------------------------------
    //
    //
    //
    // defining the main variables
    char val; // defining "val" as a variable for data coming from the serial port
    int pin1 = 1; // Forward key replacer as pin 1
    int pin2 = 2; // Backward key replacer as pin 2
    int pin3 = 3; // Left key replacer as pin 3
    int pin4 = 4; // Right key replacer as pin 4
    // -------------------------------------------------------------------------------------
    // Setting up the wiring board to work with XML server and pin directions
    void setup()
    {
    pinMode(pin1, OUTPUT); // Pin1 is going to be output
    pinMode(pin2, OUTPUT); // Pin2 is going to be output
    pinMode(pin3, OUTPUT); // Pin3 is going to be output
    pinMode(pin4, OUTPUT); // Pin4 is going to be output
    beginSerial(19200); // initialize serial port to the same speed as the broker
    }

    void serialEvent()
    {
    val = serial; // takes the data coming from the serial port & put it into a variable called “val”
    }
    // ————————————————————————————————–
    // The main program (this runs in a loop all the time)
    void loop() {
    // ********************************************
    if(val == ‘1′) // if the flash sent “1″ it means that the user pressed the “forward” key
    {
    // here is want I want the wiring board to do:
    digitalWrite(pin1, HIGH); // set pin 1 to HIGH, this will activate the 1st Optocouplers
    delay (1000); // Keep it pressed for 1 sec’
    digitalWrite(pin1, LOW); // and release it
    val =’0′; // reset the value of “val” to 0
    }
    // ********************************************
    // ********************************************
    if(val == ‘2′) // if the flash sent “2″ it means that the user pressed the “backwards” key
    {
    // here is want I want the wiring board to do:
    digitalWrite(pin2, HIGH); // set pin 2 to HIGH, this will activate the 1st Optocouplers
    delay (1000); // Keep it pressed for 1 sec’
    digitalWrite(pin2, LOW); // and release it
    val =’0′; // reset the value of “val” to 0
    }
    // ********************************************
    // ********************************************
    if(val == ‘3′) // if the flash sent “3″ it means that the user pressed the “Left” key
    {
    // here is want I want the wiring board to do:
    digitalWrite(pin3, HIGH); // set pin 3 to HIGH, this will activate the 3rd Optocouplers
    digitalWrite(pin1, HIGH); // Since our little toy car just turn the wheels ,
    // we need to press “forward” as well.
    //(like moving the stering wheel and pressing the gass pedal)
    delay (1000); // Keep it pressed for 1 sec’
    digitalWrite(pin3, LOW); // and release it
    digitalWrite(pin1, LOW); // and release it
    val =’0′; // reset the value of “val” to 0
    }
    // ********************************************
    // ********************************************
    if(val == ‘4′) // if the flash sent “4″ it means that the user pressed the “Left” key
    {
    // here is want I want the wiring board to do:
    digitalWrite(pin4, HIGH); // set pin 4 to HIGH, this will activate the 4th Optocouplers
    digitalWrite(pin1, HIGH); // Since our little toy car just turn the wheels ,
    // we need to press “forward” as well.
    //(like moving the stering wheel and pressing the gass pedal)
    delay (1000); // Keep it pressed for 1 sec’
    digitalWrite(pin4, LOW); // and release it
    digitalWrite(pin1, LOW); // and release it
    val =’0′; // reset the value of “val” to 0
    }
    // ********************************************
    }