Arduino
Thanks to Prasad Pawaskar..
1. Pot Controlled LED's
--------------------------------------------------------------
void setup() {
Serial.begin(9600);
pinMode(0,OUTPUT);
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
if(sensorValue==1023)
{
digitalWrite(0,LOW);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
}
else if( sensorValue == 20)
{
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
}
else if( sensorValue == 50)
{
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
}
else if( sensorValue == 100)
{
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
}
else if(sensorValue == 200)
{
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
}
else if(sensorValue == 300)
{
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
}
else if (sensorValue == 500)
{
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
}
else if (sensorValue == 700)
{
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
}
else if( sensorValue == 900)
{
digitalWrite(0,HIGH);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
}
else if ( sensorValue == 990 )
{
digitalWrite(0,LOW);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
}
else
{
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
}
}
--------------------------------------------------------------
2. Dancing Led's
--------------------------------------------------------------
void setup()
{
pinMode(0,OUTPUT);
pinMode(1,OUTPUT);
pinMode(3,INPUT);
pinMode(5,INPUT);
}
void loop()
{
int sw= digitalRead(3);
int sw1= digitalRead(5);
if (sw == LOW)
{
digitalWrite(0,HIGH);
digitalWrite(1,LOW);
}
else if (sw1 == LOW)
{
digitalWrite(0,LOW);
digitalWrite(1,HIGH);
}
else
{
delay(10000);
digitalWrite(0,HIGH);
delay(10000);
digitalWrite(0,LOW);
delay(10000);
digitalWrite(1,HIGH);
delay(10000);
digitalWrite(1,LOW);
}
}
--------------------------------------------------------------
3. Digital Voltmeter Arduino way
--------------------------------------------------------------
int sensorValue = 0;
void setup()
{
pinMode( 0 ,HIGH );
pinMode( 1 ,HIGH );
pinMode( 2 ,HIGH );
pinMode( 3 ,HIGH );
pinMode( 4 ,HIGH );
pinMode( 5 ,HIGH );
pinMode( 6 ,HIGH );
pinMode( 7 ,HIGH );
pinMode( 8,LOW );
}
void loop()
{
int sensorValue=analogRead(A0);
Serial.println(sensorValue);
if (sensorValue==20)
{
delay(1000);
PORTD=0b11000000;
}
else if (sensorValue==50)
{ delay(1000);
PORTD=0b11000000;
}
else if (sensorValue==50)
{ delay(1000);
PORTD=0b11000000;
}
else if (sensorValue==50)
{ delay(1000);
PORTD=0b11000000;
}
else if (sensorValue==50)
{ delay(1000);
PORTD=0b11000000;
}
else if (sensorValue==50)
{ delay(1000);
PORTD=0b11000000;
}
else if (sensorValue==50)
{ delay(1000);
PORTD=0b11000000;
}
else if (sensorValue==50)
{ delay(1000);
PORTD=0b11000000;
}
}
--------------------------------------------------------------
4. Arduino Based Shadow Alarm
--------------------------------------------------------------
// SHADOW ALARM USING ARDUINO //
int rlyPin = 12; // Relay Output Pin
int sensPin = 2; // Sensor Input Pin
int ledPin = 8; // Reminder LED Output Pin
int pzSpeaker = 10; //Piezo-speaker Output Pin
int val = 0; // variable for reading the Input Pin status
void setup() {
pinMode(rlyPin, OUTPUT); // Set Relay as output
pinMode(sensPin, INPUT); // Set Shadow Sensor as input
pinMode(ledPin, OUTPUT); // Set LED as output
pinMode(pzSpeaker, OUTPUT); // Set Piezo-Speaker as output
}
void loop(){
val = digitalRead(sensPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(rlyPin, HIGH); // turn Relay ON
digitalWrite(ledPin,HIGH); // turn LED ON
playTone(500, 600);
delay(100);
playTone(500, 800);
delay(100);
}
else {
digitalWrite(rlyPin, LOW); // turn Relay OFF
playTone(0, 0);
delay(300);
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pzSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pzSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
--------------------------------------------------------------
5. Pulse Counter using Arduino
--------------------------------------------------------------
void setup()
{
pinMode(0,HIGH);
pinMode(1,HIGH);
pinMode(2,HIGH);
pinMode(3,HIGH);
pinMode(4,HIGH);
pinMode(5,HIGH);
pinMode(6,HIGH);
pinMode(7,HIGH);
pinMode(8,LOW);
}
void loop()
{
delay(1000);
PORTD=0b11000000;
delay(1000);
PORTD=0b11111001;
delay(1000);
PORTD=0b10100100;
delay(1000);
PORTD=0b10110000;
delay(1000);
PORTD=0b10011001;
delay(1000);
PORTD=0b10010010;
delay(1000);
PORTD=0b10000010;
delay(1000);
PORTD=0b11111000;
delay(1000);
PORTD=0b10000000;
delay(1000);
PORTD=0b10011000;
}
--------------------------------------------------------------
Processing Projects
--------------------------------------------------------------
1. Processing Graph - Display a graph of the pot readings
Arduino code:
void setup() {
// initialize the serial communication:
Serial.begin(9600);
}
void loop() {
// send the value of analog input 0:
Serial.println(analogRead(A0));
// wait a bit for the analog-to-digital converter
// to stabilize after the last reading:
delay(2);
}
--------------------------------------------------------------
Processing Code:
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
void setup () {
// set the window size:
size(400, 300);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}
--------------------------------------------------------------
2. Controlling Onboard Led via processing GUI
Arduino code:
--------------------------------------------------------------
int onboardLED = 13;
void setup() {
// Begin Serial communication
Serial.begin(9600);
//Set the onboard LED to OUTPUT
pinMode(onboardLED, OUTPUT);
}
void loop(){
/* Read serial port, if the number is 0, then turn off LED
if the number is 1 or greater, turn the LED on. */
while (Serial.available() > 0) {
int num=Serial.read()-'0';
if(num<1){
digitalWrite(onboardLED, LOW); //Turn Off LED
} else{
digitalWrite(onboardLED, HIGH); //Turn On LED
}
}
}
--------------------------------------------------------------
Processing Code:
import processing.serial.*;
Serial comPort;
boolean ledState=false; //LED is off
void setup(){
//Open COM Port for Communication
comPort = new Serial(this, "COM29", 9600);
background(255,0,0); //Start with a Red background
}
void draw(){
}
void mousePressed() {
//Toggle led ON and OFF
ledState=!ledState;
//If ledState is True - then send a value=1 (ON) to Arduino
if(ledState){
background(0,255,0); //Change the background to green
/*When the background is green, transmit
a value=1 to the Arduino to turn ON LED */
comPort.write('1');
}else{
background(255,0,0); //Change background to red
comPort.write('0'); //Send "0" to turn OFF LED.
}
}
--------------------------------------------------------------
3. Controlling LED Blinking via a Text file
--------------------------------------------------------------
Arduino Code:
byte byteRead; //Used to receive data from computer.
int timeDelay; //time that the LED is On or Off
int maxtimeDelay=10000; //Maximum time delay = 10 seconds
int ledPin=13; //LED connected to pin 13 on Arduino UNO.
void setup() {
//Set pin 13 (ledPin) as an output
pinMode(ledPin, OUTPUT);
// Turn the Serial Protocol ON
Serial.begin(9600);
}
void loop() {
/* check if data has been sent from the computer: */
if (Serial.available()) {
/* read the most recent byte */
byteRead = Serial.read();
switch (byteRead) {
case 69: //This is an enquiry, send an acknowledgement
Serial.println("A");
break;
case 79: //This is an "O" to turn the LED on
digitalWrite(ledPin, HIGH);
break;
case 88: //This is an "X" to turn the LED off
digitalWrite(ledPin, LOW);
break;
case 46: //End of line
//Make sure time delay does not exceed maximum.
if(timeDelay > maxtimeDelay){
timeDelay=maxtimeDelay;
}
//Set the time for LED to be ON or OFF
delay(timeDelay);
Serial.println("S");
timeDelay=0; //Reset timeDelay;
break;
default:
//listen for numbers between 0-9
if(byteRead>47 && byteRead<58){
//number found, use this to construct the time delay.
timeDelay=(timeDelay*10)+(byteRead-48);
}
}
}
}
--------------------------------------------------------------
Processing Code:
import processing.serial.*;
Serial comPort; //The com port used between the computer and Arduino
int counter=0; // Helps to keep track of values sent.
int numItems=0; //Keep track of the number of values in text file
String comPortString; //String received From Arduino
String textFileLines[]; //Array of text file lines
String lineItems[]; //Array of line items
void setup(){
comPort = new Serial(this,"COM29", 9600); //Setup the COM port
comPort.bufferUntil('\n'); //Generate a SerialEvent when a newline is received
background(255,0,0); //Start with a Red background
}
/* Draw method is not used in this sketch */
void draw(){
}
//When the mouse is pressed, write an "E" to COM port.
//The Arduino should send back an "A" in return. This will
//generate a serialEvent - see below.
void mousePressed() {
comPort.write("E");
}
void serialEvent(Serial cPort){
comPortString = cPort.readStringUntil('\n');
if(comPortString != null) {
comPortString=trim(comPortString);
/*If the String received = A, then import the text file
change the background to Green, and start by sending the
first line of the text file to the Arduino */
if(comPortString.equals("A")){
textFileLines=loadStrings("D:/LEDdata.txt");
background(0,255,0);
sendLineNum(counter);
}
/*If the the String received = S, then increment the counter
which will allow us to send the next line in the text file.
If we have reached the end of the file, then reset the counter
and change the background colour back to red. */
if(comPortString.equals("S")){
counter++;
if(counter > (textFileLines.length-1)){
background(255,0,0);
counter=0;
} else {
sendLineNum(counter);
}
}
}
}
-----------------------------------------------------------------------
/*The sendLineNum method is used to send a specific line
from the imported text file to the Arduino. The first
line item tells the Arduino to either switch the LED on or off.
The second line item, tells the Arduino how long to keep the
LED on or off. The full-stop is sent to the Arduino to indicate
the end of the line. */
-----------------------------------------------------------------------
void sendLineNum(int lineNumber){
lineItems=splitTokens(textFileLines[lineNumber],",");
comPort.write(lineItems[0]);
comPort.write(lineItems[1]);
comPort.write(".");
}
--------------------------------------------------------------
4. Fading of Led's Via mouse movement ** Again Looks Mindblowing **
Arduino Code:
Arduino code:
void setup() {
// initialize the serial communication:
Serial.begin(9600);
}
void loop() {
// send the value of analog input 0:
Serial.println(analogRead(A0));
// wait a bit for the analog-to-digital converter
// to stabilize after the last reading:
delay(2);
}
--------------------------------------------------------------
Processing Code:
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
void setup () {
// set the window size:
size(400, 300);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}
--------------------------------------------------------------
2. Controlling Onboard Led via processing GUI
Arduino code:
--------------------------------------------------------------
int onboardLED = 13;
void setup() {
// Begin Serial communication
Serial.begin(9600);
//Set the onboard LED to OUTPUT
pinMode(onboardLED, OUTPUT);
}
void loop(){
/* Read serial port, if the number is 0, then turn off LED
if the number is 1 or greater, turn the LED on. */
while (Serial.available() > 0) {
int num=Serial.read()-'0';
if(num<1){
digitalWrite(onboardLED, LOW); //Turn Off LED
} else{
digitalWrite(onboardLED, HIGH); //Turn On LED
}
}
}
--------------------------------------------------------------
Processing Code:
import processing.serial.*;
Serial comPort;
boolean ledState=false; //LED is off
void setup(){
//Open COM Port for Communication
comPort = new Serial(this, "COM29", 9600);
background(255,0,0); //Start with a Red background
}
void draw(){
}
void mousePressed() {
//Toggle led ON and OFF
ledState=!ledState;
//If ledState is True - then send a value=1 (ON) to Arduino
if(ledState){
background(0,255,0); //Change the background to green
/*When the background is green, transmit
a value=1 to the Arduino to turn ON LED */
comPort.write('1');
}else{
background(255,0,0); //Change background to red
comPort.write('0'); //Send "0" to turn OFF LED.
}
}
--------------------------------------------------------------
3. Controlling LED Blinking via a Text file
--------------------------------------------------------------
Arduino Code:
byte byteRead; //Used to receive data from computer.
int timeDelay; //time that the LED is On or Off
int maxtimeDelay=10000; //Maximum time delay = 10 seconds
int ledPin=13; //LED connected to pin 13 on Arduino UNO.
void setup() {
//Set pin 13 (ledPin) as an output
pinMode(ledPin, OUTPUT);
// Turn the Serial Protocol ON
Serial.begin(9600);
}
void loop() {
/* check if data has been sent from the computer: */
if (Serial.available()) {
/* read the most recent byte */
byteRead = Serial.read();
switch (byteRead) {
case 69: //This is an enquiry, send an acknowledgement
Serial.println("A");
break;
case 79: //This is an "O" to turn the LED on
digitalWrite(ledPin, HIGH);
break;
case 88: //This is an "X" to turn the LED off
digitalWrite(ledPin, LOW);
break;
case 46: //End of line
//Make sure time delay does not exceed maximum.
if(timeDelay > maxtimeDelay){
timeDelay=maxtimeDelay;
}
//Set the time for LED to be ON or OFF
delay(timeDelay);
Serial.println("S");
timeDelay=0; //Reset timeDelay;
break;
default:
//listen for numbers between 0-9
if(byteRead>47 && byteRead<58){
//number found, use this to construct the time delay.
timeDelay=(timeDelay*10)+(byteRead-48);
}
}
}
}
--------------------------------------------------------------
Processing Code:
import processing.serial.*;
Serial comPort; //The com port used between the computer and Arduino
int counter=0; // Helps to keep track of values sent.
int numItems=0; //Keep track of the number of values in text file
String comPortString; //String received From Arduino
String textFileLines[]; //Array of text file lines
String lineItems[]; //Array of line items
void setup(){
comPort = new Serial(this,"COM29", 9600); //Setup the COM port
comPort.bufferUntil('\n'); //Generate a SerialEvent when a newline is received
background(255,0,0); //Start with a Red background
}
/* Draw method is not used in this sketch */
void draw(){
}
//When the mouse is pressed, write an "E" to COM port.
//The Arduino should send back an "A" in return. This will
//generate a serialEvent - see below.
void mousePressed() {
comPort.write("E");
}
void serialEvent(Serial cPort){
comPortString = cPort.readStringUntil('\n');
if(comPortString != null) {
comPortString=trim(comPortString);
/*If the String received = A, then import the text file
change the background to Green, and start by sending the
first line of the text file to the Arduino */
if(comPortString.equals("A")){
textFileLines=loadStrings("D:/LEDdata.txt");
background(0,255,0);
sendLineNum(counter);
}
/*If the the String received = S, then increment the counter
which will allow us to send the next line in the text file.
If we have reached the end of the file, then reset the counter
and change the background colour back to red. */
if(comPortString.equals("S")){
counter++;
if(counter > (textFileLines.length-1)){
background(255,0,0);
counter=0;
} else {
sendLineNum(counter);
}
}
}
}
-----------------------------------------------------------------------
/*The sendLineNum method is used to send a specific line
from the imported text file to the Arduino. The first
line item tells the Arduino to either switch the LED on or off.
The second line item, tells the Arduino how long to keep the
LED on or off. The full-stop is sent to the Arduino to indicate
the end of the line. */
-----------------------------------------------------------------------
void sendLineNum(int lineNumber){
lineItems=splitTokens(textFileLines[lineNumber],",");
comPort.write(lineItems[0]);
comPort.write(lineItems[1]);
comPort.write(".");
}
--------------------------------------------------------------
4. Fading of Led's Via mouse movement ** Again Looks Mindblowing **
Arduino Code:
--------------------------------------------------------------
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, HIGH);
}
if(byteRead>0){
//Turn on the relevant LEDs
for(int i=1; i<(byteRead+1); i++){
digitalWrite(i+1, LOW);
}
}
}
}
--------------------------------------------------------------
Processing Code:
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, "COM29", 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);
}
}
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, HIGH);
}
if(byteRead>0){
//Turn on the relevant LEDs
for(int i=1; i<(byteRead+1); i++){
digitalWrite(i+1, LOW);
}
}
}
}
--------------------------------------------------------------
Processing Code:
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, "COM29", 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