Mapping light sensor readings and drawing interactive circle from values

edited December 2016 in Arduino

Hello :)

I am trying to map light sensor readings to the range 0-255, and draw an interactive circle from the values. What I am experiencing is that the values look a bit odd (either 10.0 or 119.0) and they are blinking together with the blinking TX light on my arduino uno. There seems to be no effect when covering the light sensor or exposing it to more light.

Originally, I set up the arduino arrangement using the Arduino IDE just reading and mapping the values and sending them to the Serial monitor. They seemed reasonable; highest light value about 700 and lowest value with my hand covering about 100. These were mapped well to 0-255. I could, however, only manage to display these with Serial.println and when I tried Serial.write black diamond shapes with question marks appeared instead.

I have closed the Arduino IDE, I have double checked that usb port is the right one... I am using a mac and the exercise workbook that advised I might need to add the following code to Terminal.app

sudo mkdir -p /var/lock sudo chmod 777 /var/lock

which I also added (hope this is sensible and that I haven't messed up my computer).

The code I am trying with is:

import processing.serial.*;

Serial port;
float value=0;
float maxsize=200;
int ysize=int(maxsize+100);
int xsize=int(maxsize+100);
float diam;

void setup()
{
 size(200,200);
 stroke(0,255,0);
 noFill();
 strokeWeight(2);
 smooth();

 println(Serial.list());

 port=new Serial(this,Serial.list()[11],9600); //have doubled checked that the port is the right one
}

void draw()
{
  background(107,17,77);

 if(0<port.available())
 {
  value=port.read(); 
 }
 println(value);

 diam=map(value,0,255,0,maxsize);

 pushMatrix();
 translate(width/2,height/2);
 ellipse(0,0,diam,diam);
 popMatrix();
}

I am quite new to programming and any help will be very much appreciated! Thank you so much and happy holidays.

Answers

  • Answer ✓

    Your reading your arduino data from processing without splitting your data input. You might need something like:

    https://processing.org/reference/libraries/serial/Serial_readBytesUntil_.html

    or to manually check the content of value @ line 29 to split the data stream into valid tokens. I am guessing you are appending every value with a new line separator. I will also suggest to move line 25 into the if block in line 27: you want to update the diameter only upon receiving new data. Otherwise if your data stream rate is slower than draw() rate, you effect will be displayed for short periods of time. This is just a suggestion but you might use it the way it is right now.

    The mapping from range [100,700] to [0,255] is being done in your arduino code?

    Kf

Sign In or Register to comment.