Etch-a-Sketch
- Posted by droops on September 24th, 2008
Arduino Floating Ball from droops on Vimeo.
Arduino Etch-a-Sketch from droops on Vimeo.
I had seen some similar things on the internet, but I wanted to make my own. I took 3 pots hooked up to analog inputs on my Arduino and then sent a serial connection that looked like this:
X56
Y378
Z5
X68
Y378
Z6
Using Processing, I read that stream and displayed the variables on the screen. So you can change the horizontal, vertical, and size of the drawing dot. I also included a grey dot in the middle of the big dot, so you have a reference of where your dot is. This code isn’t fancy and allows for many upgrades. Most importantly, I am not redrawing the background to allow for the green dots to draw lines, don’t worry, I left a few comments to help you out.
All you need is a duino, 3 pots, processing, and the arduino ide.
/*
Duino Etch-a-Sketch
by droops
http://dailyduino.com
I used sources from all over the internet, so thanks for the code, please use mine
Sometimes it doesnt run and will dump out an error, just try to run it again before
you get all crazy and start throwing things
*/
import processing.serial.*;
Serial myPort;
String inString; // Input string from serial port:
String letter;
int number;
String numberS;
int x, y, z;
int redColor = 12;
int blueColor = 170;
int greenColor = 21;
int lf = 10;
void setup() {
size(512, 512);
//you may have to change the serial port
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(lf);
background(0);
}
void draw() {
// background(0); //uncomment this to just have a floating ball
stroke(redColor, blueColor, greenColor); //changing colors are for version 2
fill(redColor, blueColor, greenColor);
smooth();
ellipse(x, y, z, z); //this is the circle
stroke(255); //color of point
point(x, y); //point inside circle, comment this to remove
}
void serialEvent(Serial p) {
inString = (myPort.readString());
letter = inString.substring(0, 1);
numberS = inString.substring(2);
numberS = trim(numberS);
number = Integer.parseInt(numberS); //I could have combined a bit of this, but this code is for learning
if(letter.equals("X") == true) {
x = number;
}
if(letter.equals("Y") == true) {
y = number;
}
if(letter.equals("Z") == true) {
z = number;
}
}
/*
//Arduino Code for Etch-a-Sketch
int rightPin = 3; //analog pin for right hand knob
int leftPin = 2; //analog pin for left hand knob
int widthPin = 1; //analog pin for center knob (width)
//Info for Serial Data
int id_1 = 'X';
int id_2 = 'Y';
int id_3 = 'Z';
int space = ' ';
void setup()
{
pinMode(rightPin, INPUT);
pinMode(leftPin, INPUT);
pinMode(widthPin, INPUT);
Serial.begin(9600);
}
void loop()
{
int rightNum = analogRead(rightPin);
int leftNum = analogRead(leftPin);
int widthNum = analogRead(widthPin);
//the arduino will read from 0 to 1024 on teh analog pins, so we divide that by 2
//to fit in the 512x512 screen
//by doing this simple math on the arduino, it may make things faster
rightNum = rightNum / 2;
leftNum = leftNum / 2;
//we divide the widthNum by 50 so that the elipse can be a bit bigger, play with this number
widthNum = widthNum / 50;
printByte(id_1);
printByte(space);
printInteger(rightNum); printByte(10);
printByte(id_2);
printByte(space);
printInteger(leftNum); printByte(10);
printByte(id_3);
printByte(space);
printInteger(widthNum); printByte(10);
delay(10);
}
*/


