Home‎ > ‎Unit 06‎ > ‎

User Input and Blink

Having Processing control an LED is cool, but we can make it cooler. Why not use the keyboard or mouse to control whether the LED in on or off?

Try this code:
import processing.serial.*; 
import cc.arduino.*;    

Arduino arduino;      

int ledPin = 13;  

void setup() {
  size(100, 100);    
  arduino = new Arduino(this, Arduino.list()[0], 57600);  
  arduino.pinMode(ledPin, Arduino.OUTPUT);

void draw()
  if (keyPressed){
    if ((key == 'o')|| (key == 'O'))
      arduino.digitalWrite(ledPin, Arduino.HIGH);
    else if ((key == 'p')|| (key == 'P'))
      arduino.digitalWrite(ledPin, Arduino.LOW);

Assignment 6.1 - Change this program so that it is controlled by the Arrow Keys. Up for on and Down for off.

Assignment 6.2 - Make the LED flash, but change it so that every time up press the up arrow the flashing rate increases and when you use the down arrow the flashing rate slows down. (Note you'll probably need a variable to use for your delay and then do some math with variables). 

Assignment 6.3 - Then change the flash rate based upon the mouse position in your sketch window. (You'll probably want to use map to get a good range of flash rates)