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
- Processing Program installed on computer
- Arduino Program installed on the computer
Arduino Sketch
This was made using Fritzing.
Arduino Code
You can download the Arduino IDE from this site.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | /* 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); } } } } |
The code above was formatted using this site.
Processing Code
You can download the Processing IDE from this site.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | //Created by ScottC on 12/05/2012 to send mouse coordinates to Arduino import processing.serial.*; // Global variables int new_sX, old_sX; int nX, nY; Serial myPort; // Setup the Processing Canvas void setup(){ size( 800, 400 ); strokeWeight( 10 ); //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); } } |
The code above was formatted using this site.
Nice work. Do you know how i can use the Processing program to read from a usb controller? a controller with x,y, and z coordinates then move 3 servos with arduino depending on the location of each coordinate?
ReplyDeleteHi Anonymous,
DeleteI don't have a USB controller - so cannot help you there. Perhaps ask in the "Processing Forum".. as you are more likely to get a useful answer there.
But once you get the coordinates into Processing, you can easily send them to the Arduino. Have a look at one of my other tutorials in the Arduino Basics Project page (Serial tutorial 1 & 2) on the right hand side menu.
Thanks for the reply, can you tell me how change the code of the arduino so that it moves a servo instead of the LEDs. I was trying to figure it out but ran into some problems.
DeleteThanks
Hi Anonymous:
DeleteLooks like someone has already done this... have a look at
http://www.youtube.com/watch?v=2wx6kl9kHLY
They will be able to tell you how they did it.
hey, where you download Mouse libraries?
ReplyDeleteYou don't use any in this project. However you do need the Processing IDE
DeleteAre you able to modify this to have a larger range of leds (if so, how?), or are you limited to 9?
ReplyDeleteThe 2 most common options for increasing the number of LEDs is to:
Delete- Use shift registers
- Use an Arduino MEGA.
Have a look at this YouTube video- where this person uses and Arduino UNO to control an 8x8x8 LED cube.
The code displays no error, however everytime I try to run the code, the window shows up however it does not work like when i try to move my mouse it doesn't sense or show that it is sensing . i checked my circuit and arduino code and that is not the problem. do you have any idea what the problem could be.
ReplyDeleteYou have to move your mouse within that window that shows up.
DeleteIf it does not show a line when you move the mouse within that window, then you have an issue within the Processing code. It may even be an issue with the version of the Processing IDE. I did this project a while ago, so you will need to troubleshoot using trial and error to find the part of the code that is not working for you.
Hi how do you explain this code
ReplyDelete{
byte byteRead;
if (Serial.available()) {
byteRead = Serial.read();
byteRead = byteRead - '0';
for (int i = 3; i < 10; i++) {
digitalWrite(i, LOW);
}
if (byteRead > 0) {
for (int i = 1; i < (byteRead + 1); i++) {
digitalWrite(i + 1, HIGH);
}
}
}
}
Read the character through Serial communication. Convert the character to an integer by using this trick -
DeletebyteRead = byteRead - '0';
You need to understand the ASCII codes for numbers to understand that trick.
Then turn OFF all LEDs attached to pins 3 to 9 (make them LOW).
Not sure why this code was changed because the next set of code does not match... but anyway - it says that it will turn ON all LEDs attached to pins 1 to byteRead (+1)... so that means if byteRead is equal to 5. It will turn ON all LEDS attached to pins 1 to 6.
What is the experimental Procedure to this??
ReplyDeletenot sure what you are asking ?
DeleteSo Ive tried this project and like when I type the processing code in the arduino like it won't work, and I checked the circuit it's correct so I'm just wondering what's the problem with it?(btw I'm using a Mac computer if ever there's a difference in use of the computer)(btw this is my project and it's due on Jan 19 so hope u can answer asap :)))
ReplyDeleteThe processing code is not run on the Arduino.
DeleteIt is run within the Processing IDE which is run on your computer.
The Arduino Code is uploaded to your Arduino.
Processing and Arduino IDE are two seperate programs but look very similar in appearance.
If any of you ever come across a project using air mouse and arduino UNO board to control robotic arm, please let me know. Thanks! Have a great day
ReplyDelete