Home‎ > ‎Unit 08‎ > ‎Etch-a-Sketch‎ > ‎

Delay Fix

This code introduces a boolean variable. Booleans can be true or false. During our first run we want to wait for the Arduino to catch up so we pause and update pxPos and pyPos. We only need to do this the first time through the sketch though. There's a 2 second delay. That may or may not be long enough. If you still start in the top left corner then make the delay longer.

import processing.serial.*; //Import the serial library into your sketch
import cc.arduino.*;        //Import the Arduino-Firmata library into your sketch

Arduino arduino;      //Create an instance of Arduino named arduino (can be any name) 
float xPos;
float yPos;
float pxPos;
float pyPos;
boolean firstRun=true;

void setup()
{
  size(400, 400);
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  smooth();
  strokeWeight(4);
  stroke(240);
  background(0);
}

void draw()
{
  if (firstRun)
  {
    delay(2000);  //adjust as needed
    pxPos= map(arduino.analogRead(2), 0, 1023, 5, width-5);
    pyPos = map(arduino.analogRead(1), 0, 1023, 5, height-5);
    firstRun=false;
  }
  xPos = map(arduino.analogRead(2), 0, 1023, 5, width-5);
  yPos = map(arduino.analogRead(1), 0, 1023, 5, height-5);
  line (xPos, yPos, pxPos, pyPos);
  pxPos=xPos;  //update pxPos
  pyPos=yPos;  //update pyPos
}


Comments