Skip to content

Instantly share code, notes, and snippets.

@santolucito
Last active October 14, 2021 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santolucito/327bbc97e2e1bc01bb27cd85bd62e95f to your computer and use it in GitHub Desktop.
Save santolucito/327bbc97e2e1bc01bb27cd85bd62e95f to your computer and use it in GitHub Desktop.
Pressure sensor w/ Arduino + Processing
Video demo and explanation here: https://youtu.be/CYUlLaxY4Xs
/**
* Simple Read
*
* Read data from the serial port and change the color of a rectangle
* when a sensor connected to an Arduino board is changed.
*/
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
int lf = 10; // ASCII linefeed
void setup()
{
size(200, 200);
// Open whatever port is the one you're using.
print(Serial.list());
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
}
void draw()
{
String s = "";
if ( myPort.available() > 0) { // If data is available,
s = myPort.readString();
try {
s = s.split("\n")[1].strip();
val = Integer.parseInt(s);
}
catch (Exception e) {
println(e);
}
}
background(0);
fill(val,255-val,val);
println(val);
rect(50, 50, val, val);
}
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the sensor
int outputValue = 0; // value output to the serial line
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
//Serial.println(sensorValue);
//you might need to adjust the range depending on your sensor
outputValue = map(sensorValue, 335, 355, 0, 255);
// print the results to the Serial Monitor:
Serial.println(outputValue);
// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after ○the last reading:
delay(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment