Close

januari 29, 2016

Etch a sketch on the computer with Arduino

Trying to simulate this good old fashioned toy, the Etch-a-Sketch, on my computer.
I had lots of difficulties, like the Serial port wasn’t responding right, I got this annoying ‘null point exception’ in Progressing, but in the end I succeeded!
Many thanks to all the examples and help I found on the internet.

etch_a_1024_3

So here’s the sketch on breadboard:

etch_a_1024

As you see, very easy. Only two potentiometers and some wiring.

etch_a_1024_2

So, this is the code:

/**
 * Data from multiple sensors / Processing 
 * by susan odendaal, based on examples at BARRAGAN <http://barraganstudio.com>
 * and based on Tom Igoe's example from Making Things Talk book
 * (ALSO BASED ON EXAMPLE ON GITHUB: https://github.com/arduino/Arduino/issues/4214)
 *
 * read serial data until a linefeed character is found
 * data are values comma separated. Split the data and convert it
 * into numbers in an array for fUrther use.
*/

/**
2 POTENTIOMETERS, WITH THE WIPER ATTACHED TO ANALOG A0 AND THE ANALOG A1 OF THE ARDUINO.
THE OTHER CONTACTS TO + AND -
*/

//CODE IN ARDUINO IDE **************
/**

float actsensor0;
float actsensor1;
//**these values need to be exactly specified in Processing also:
float width = 1030;
float height = 700;
float margin = 50;
//**
void setup() {
 // initialize serial communication
 Serial.begin(9600);
}

void loop() {
 // read the value of A0 and A1, trim them so they fit in the drawing with marges, and
 // send it as a byte over the serial connection

 actsensor0 = analogRead(A0)*(height/1023)/height*(height-(margin)*2)+margin;
 int actsensor0int = int(actsensor0);
 actsensor1 = analogRead(A1)*(width/1023)/width*(width-(margin)*2)+margin;
 int actsensor1int = int(actsensor1);


Serial.print(actsensor0int); //vertical
Serial.print(",");
Serial.println(actsensor1int);//horizontal
 
delay(250);
}
}
//************************************

*/

import processing.serial.*;

Serial myPort; 
int numSensors = 2; // we will be expecting for reading data from two sensors
int sensors[]; // array to read the 2 values
int pSensors[]; // array to store the previuos reading, usefur for comparing actual reading with the last one

boolean start = true; //needed for doing some stuff only at the startup

int margin = 20;
int margin_large = 50; //this one also to be specified in the Arduino code
//float inByte = 0;


void setup() 
{

 size(1030, 700); //this one also to be specified in the Arduino code
 
 myPort = new Serial(this, "/dev/tty.usbmodemFD141", 9600); 
 // read bytes into a buffer until you get a linefeed (ASCII 10):
 myPort.bufferUntil('\n');

 //draw the background
 background (200);
 fill(#9D0606);//red
 rect (margin,margin, width-(margin*2),height-(margin*2),25);
 fill(124);//grey
 rect (margin_large,margin_large, width-(margin_large*2),height-(margin_large*2),15);
 
 int prex = pSensors[1];
 int prey = pSensors[0];
 
 //starting point of the "cursor":
 strokeWeight(3);
 line (prex-5, prey, prex+5, prey);
 line (prex,prey-5,prex,prey+5);
}

void draw() 
{
 int x = sensors[1];//the horizontal
 int y = sensors[0];//the vertical
 int prex = pSensors[1];
 int prey = pSensors[0];
 
 if (start == true)
 {
 if ((x - prex != 0) || (y - prey != 0))
 { 
 //remove the cursor 
 stroke(124);
 line (prex-5, prey, prex+5, prey);
 line (prex,prey-5,prex,prey+5);
 start = false;
 // set the settings of the drawer:
 stroke(0);//black
 strokeWeight(1);
 }
 }

 // if valid data arrays are not null
 // compare each sensor value with the previuos reading 
 // to establish change
 
 if((pSensors != null)&&(sensors != null)) 
 { 
 for(int i=0; i < numSensors; i++) 
 {
 int delta = sensors[i] - pSensors[i]; // actual - previous value
 if (delta !=0 )
 {
 line (prex,prey,x,y);
 }
 }
 }
}

void serialEvent(Serial myPort) 
{
 // read the serial buffer:
 String myString = myPort.readStringUntil('\n');

 // if you got any bytes other than the linefeed:
 if (myString != null) 
 {
 myString = trim(myString);
 pSensors = sensors;
 // split the string at the comma
 // and convert the sections into integers:
 sensors = int(split(myString, ','));

 // print out the values you got for debugging purposes:
 for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) 
 {
 print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
 }
 // add a linefeed after all the sensor values are printed:
 println();
 }
}

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *