this program takes analog input from a pot on A0 and sends it to a windows application for drawing a colored background. analogread() is really slow so I try to call it only once. this greatly improved performance. close the arduino IDE after you download your code so you don't have a conflict of two applications using the same serial port. you can still monitor your serial from the processing window. you must run your app in processing after your sketch has started running on the arduino.
put this on the arduino
/*
PWM fade the LED on pin 13 controlled by a pot on A0
*/
int sensorPin = A0;
int ledPin = 13;
int potValue;
int potValueDiv;
int potValueDiv2;
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(0, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(28800);
}
void loop() {
potValue = analogRead(sensorPin);
potValueDiv = potValue/4;
potValueDiv2 = potValue/32;
digitalWrite(ledPin, HIGH); // set the LED on
delay(potValueDiv2); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(32-potValueDiv2); // wait for a second
Serial.write(potValueDiv);
}
this is the code for the processing language
download processing from processing.org
import processing.serial.*;
int[] dataIn = new int[2];
Serial port;
int counter;
void setup()
{
port = new Serial(this, Serial.list()[1], 28800);
size(400, 400);
colorMode(HSB, 256);
}
void draw()
{
if(port.available() > 0)
{
dataIn[counter] = port.read();
println(dataIn[counter]);
background(dataIn[counter], 255, 255);
counter = (counter+1) % 1;
}
}
I have only had my arduino for 3 days and I have done many projects already. really easy to use