Open monicaflores10 opened 7 years ago
It's probably because on line 18 you have waterLevel = 100;
inside your draw loop. You could just get rid of that line completely and instead initialize waterLevel
with a value on line 3 where you declare it.
var serial;
var randColor;
var waterLevel = 100;
function setup() {
createCanvas(400, 400);
serial = new p5.SerialPort();
serial.open('/dev/cu.usbmodem1421');
serial.on('data', parseData);
//serial.on('error', printError);
}
function draw() {
background(255);
randColor = random(255)
stroke(randColor, 90, 24);
strokeWeight(4);
fill(0);
ellipse(width / 2, height / 2, waterLevel);
}
function parseData() {
var data = serial.readLine();
console.log(data);
if (data.length > 0) {
waterLevel = int(map(data, 0, 630, 100, 400));
}
}
https://alpha.editor.p5js.org/monicaflores10/sketches/ByWICvaaZ
I have data that comes up on the console when I dip my water level sensor in water that has a range of 0-600. I've mapped the water level data to the height and width of my ellipse, yet when I dip my sensor in the water the ellipse's height and width doesn't change. Not sure why my ellipse is static.
This is my Arduino code:
void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); pinMode(A0, INPUT); }
void loop() { int waterLevel = analogRead(A0); // print the results to the Serial Monitor: Serial.println(waterLevel);
// wait 2 milliseconds before the next loop for the analog-to-digital // converter to settle after the last reading: delay(2); }