Use a mouse to control LEDs attached to an Arduino. This project uses the processing language to transmit the mouse coordinates to the Arduino, which then uses this information to turn on some LEDs. Please see the video below to see it in action.
Components Required for this project:
Arduino UNO
Breadboard
9 LEDs
9 x 330 ohm resistors
Wires to connect the circuit
USB connection cable: to connect the computer to the Arduino
A computer: to run the processing sketch, and to compile / upload the Arduino sketch
/* This program was created by ScottC on 9/5/2012 to receive serial signals from a computer to turn on/off 1-9 LEDs */
void setup() { // initialize the digital pins as an output. pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); // Turn the Serial Protocol ON Serial.begin(9600); }
void loop() { byte byteRead;
/* check if data has been sent from the computer: */ if (Serial.available()) {
/* read the most recent byte */ byteRead = Serial.read(); //You have to subtract '0' from the read Byte to convert from text to a number. byteRead=byteRead-'0';
//Turn off all LEDS for(int i=2; i<11; i++){ digitalWrite(i, LOW); }
if(byteRead>0){ //Turn on the relevant LEDs for(int i=1; i<(byteRead+1); i++){ digitalWrite(i+1, HIGH); } } } }
//Open the serial port for communication with the Arduino //Make sure the COM port is correct myPort = new Serial(this, "COM6", 9600); myPort.bufferUntil('\n'); }
// Draw the Window on the computer screen void draw(){
// Fill canvas grey background( 100 );
// Set the stroke colour to white stroke(255);
// Draw a circle at the mouse location ellipse( nX, nY, 10, 10 );
//Draw Line from the top of the page to the bottom of the page //in line with the mouse. line(nX,0,nX,height); }
// Get the new mouse location and send it to the arduino void mouseMoved(){ nX = mouseX; nY = mouseY;
//map the mouse x coordinates to the LEDs on the Arduino. new_sX=(int)map(nX,0,800,0,10);
if(new_sX==old_sX){ //do nothing } else { //only send values to the Arduino when the new X coordinates are different. old_sX = new_sX; myPort.write(""+new_sX); } }
No comments:
Post a Comment