Skip to content

Instantly share code, notes, and snippets.

@ebot
Created March 7, 2013 00:56
Show Gist options
  • Save ebot/5104645 to your computer and use it in GitHub Desktop.
Save ebot/5104645 to your computer and use it in GitHub Desktop.
MyDimmer example for sending data to an Arduino board through the serial port with Processing.
/*
MyDimmer
My hack of the script created by David Mellis, Tom Igoe and Scott Fitzgerald.
http://www.arduino.cc/en/Tutorial/Dimmer
The script to reads bytes of input from the serial port and uses it to contol the brightness of an LED on pin 9.
Mine lights an additional indicator LED on pin 7 as an indicator.
The circuit:
LED attached from digital pin 9 to ground.
LED attached from digital pin 7 to ground.
I used Processing for the serial connection
http://processing.org/
*/
const int dimmer_pin = 9; // The pin that the LED is attached to
const int indicator_pin = 7; // The pin that will idicate that we are running
void setup()
{
// Initialize the serial communication:
Serial.begin(9600);
// Initialize the pins as output:
pinMode(dimmer_pin, OUTPUT);
pinMode(indicator_pin, OUTPUT);
}
void loop() {
byte brightness; // Variable that holds the value of the brightness sent from serial.
// Turn on the indicator pin
digitalWrite(indicator_pin, HIGH);
// Check if data has been sent from the computer:
if (Serial.available()) {
// Read the most recent byte (which will be from 0 to 255):
brightness = Serial.read();
// Set the brightness of the LED:
analogWrite(dimmer_pin, brightness);
}
}
/* Processing code for this example
* Dimmer - sends bytes over a serial port
* by David A. Mellis
* This example code is in the public domain.
*/
import processing.serial.*;
Serial port;
void setup() {
size(256, 150);
println("Available serial ports:");
println(Serial.list());
// Uses the first port in this list (number 0). Change this to
// select the port corresponding to your Arduino board. The last
// parameter (e.g. 9600) is the speed of the communication. It
// has to correspond to the value passed to Serial.begin() in your
// Arduino sketch.
port = new Serial(this, Serial.list()[4], 9600);
}
void draw() {
// draw a gradient from black to white
for (int i = 0; i < 256; i++) {
stroke(i);
line(i, 0, i, 150);
}
// write the current X-position of the mouse to the serial port as
// a single byte
// println(mouseX);
port.write(mouseX);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment