Hi everyone, I’m working on this code, my aim is to print on the screen the value of C (that works) and the value of h (that compute from C). However, when I run the code, this latter value is not shown on the screen.
Someone can help me ?
thanks, the whole code of processing is this below
import processing.serial.*;
// upper and lower capacitance ranges on which to switch on LEDs
//int grn_led_hi = 100;
//int grn_led_lo = 40;
//int amb_led_hi = 39;
//int amb_led_lo = 26;
int red_led_hi = 10;
int red_led_lo = 1;
Serial ser_port; // for serial port
char rx_byte; // stores single byte received from serial port
String str_capacitance = ""; // stores received capacitance string
boolean got_cap = false; // flags received capacitance
PFont fnt; // for font
int[] graph; // graph array
int graph_x = 0; // index into graph array
char g_on_led = 'N'; // switches on or off LEDs in window
float a = 0.15; //lato lungo rettangolo
float b = 0.05; //lato corto rettangolo
float d = 0.09; //distanza tra le armature
float d_1 = 0.002; //distanza tra le armature
float e0 = 8.85 * pow(10, -12); //permittività vuoto
float er = 78; //permittività acqua
float erv = 7; //permittività vetro
float capacitance=0;
float h = 0;
String altezza = "";
int buzzer=7;
PImage img; //variabile dell'immagine della formula
PShape bottiglia;
float ry;
void setup() {
size(800, 700, P3D); // size of application window
background(0); // black background
img = loadImage("formula.jpg"); // Load the image into the program
bottiglia = loadShape("bottiglia.obj");
fnt = createFont("Arial", 16, true); // font displayed in window
//println(Serial.list());
ser_port = new Serial(this, Serial.list()[0], 9600);
//ser_port.pinMode(buzzer, ser_port.INPUT);
// draw initial visual elements
DrawGraphAxis();
//drawbottiglia();
DrawLEDs('N');
// initialize the graph array
graph = new int[500];
for (int i = 0; i < 500; i++) {
graph[i] = -100; // -100 indicates invalid value
}
}
void draw()
{
if (got_cap) { // capacitance received from serial port
pushMatrix();
drawbottiglia();
popMatrix();
DrawGraphAxis();
got_cap = false; // reset flag
// display the current capacitance in a box
noFill();
stroke(255);
rect (220, 300, 120, 30, 7);
rect (220, 350, 180, 80, 7);
rect (70, 450, 120, 30, 7);
textFont(fnt, 16);
str_capacitance += "pF";
text (str_capacitance, 230, 320); // current capacitance
altezza += "cm";
text(altezza, 100, 470 );
image(img, 230, 365);
DrawGraph(str_capacitance, altezza);
DrawLEDs(g_on_led);
str_capacitance = ""; // reset capacitance sting
altezza = "";
}
}
// called when data has been received on the serial port
void serialEvent(Serial p)
{
//String inString =ser_port.readStringUntil('\n');
while (p.available() > 0) {
// get a single character from the serial port
rx_byte = ser_port.readChar();
//rx_byte=trim(rx_byte);
//float[] v=float(split(rx_byte, '\n'));
//capacitance = v[0];
//h=v[1];
//println(capacitance);
// only use characters 0 to 9 and .
if ((rx_byte >= '0' && rx_byte <= '9') || (rx_byte == '.')) {
str_capacitance += rx_byte; // build the capacitance string
}
// capacitance received?
if ((rx_byte == '\n') && (str_capacitance.length() != 0)) {
got_cap = true; // flag received capacitance
}
}
}
void DrawGraphAxis()
{
stroke(255);
line(65, 240, 640, 240); // horizontal axis
line(76, 245, 76, 20); // vertical axis
// temperature text on y axis in degrees Celsius
textFont(fnt, 14);
fill (255); //colore banco delle scritte sottostanti
//asse y
text ("0", 45, 245);
text ("2", 45, 225);
text ("4", 45, 205);
text ("6", 45, 185);
text ("8", 45, 165);
text ("10", 40, 145);
text ("12", 40, 125);
text ("14", 40, 105);
text ("16", 40, 85);
text ("18", 40, 65);
text ("20", 40, 45);
text ("h(cm)", 25, 20);
text ("C(pF)", 625, 260);
//point(77, 240);
text ("10", 121, 260);
text ("20", 165, 260);
text ("30", 209, 260);
text ("40", 253, 260);
text ("50", 297, 260);
text ("60", 341, 260);
text ("70", 385, 260);
text ("80", 429, 260);
text ("90", 473, 260);
text ("100", 517, 260);
//righe non orizzontali non spesse
// draw the horizontal lines next to each capacitance on the graph
stroke(50);
//line(71, 240, 580, 240); // skip 0
line(71, 220, 580, 220);
line(71, 200, 580, 200);
line(71, 180, 580, 180);
line(71, 160, 580, 160);
line(71, 140, 580, 140);
line(71, 120, 580, 120);
line(71, 100, 580, 100);
line(71, 80, 580, 80);
line(71, 60, 580, 60);
line(71, 40, 580, 40);
}
//SEMAFORO
// draws the 3 LEDs on the screen
// on_led can be R, G or A to switch on the red, green or amber LEDs
// on_led can be anything else, e.g. N to switch all LEDs off
void DrawLEDs (char on_led)
{
fill(0);
stroke(0);
//rect (70, 300, 120, 30, 7); // box around LEDs on screen
//noStroke();
if (on_led == 'R') {
fill(255, 0, 0); // LED on
} else {
fill(40, 0, 0); // LED off
}
ellipse(110, 355, 100, 100); // green LED
//if (on_led == 'A') {
// fill(255, 255, 0);
//} else {
// fill(40, 40, 0);
//}
//ellipse(130, 315, 16, 16); // amber LED
//if (on_led == 'R') {
// fill(255, 0, 0);
//} else {
// fill(40, 0, 0);
//}
//ellipse(170, 315, 16, 16); // red LED
}
void DrawGraph(String temp, String altezza)
{
int y_start; // temporary index into graph array when drawing graph
graph[graph_x] = int(temp); // add the newest capacitance to the graph array
// check if an LED must be switched on
if (graph[graph_x] <= red_led_hi && graph[graph_x] >= red_led_lo) {
g_on_led = 'R';
ser_port.write('R');
rect (220, 550, 180, 80, 7);
textFont(fnt, 16);
text ("Riempi la bottiglia!!!", 240, 590); // current capacitance
}
//else if (graph[graph_x] <= amb_led_hi && graph[graph_x] >= amb_led_lo) {
// g_on_led = 'A';
// ser_port.write('A');
//}
//else if (graph[graph_x] <= red_led_hi && graph[graph_x] >= red_led_lo) {
// g_on_led = 'R';
// ser_port.write('R');
//}
else {
g_on_led = 'N';
ser_port.write('N');
}
// point to next position in graph array, or reset to 0 if at end of array
// this the pointer in the circular buffer graph[]
graph_x++;
if (graph_x >= 500) {
graph_x = 0;
}
y_start = graph_x; // temporary copy of graph index
stroke(255, 0, 0); // graph colour
// draw the graph
for (int i = 0; i < 500; i++) {
if (graph[y_start] >= -30) { // only draw valid values
float h = (( graph[y_start] * pow(10, -12) * d) / (e0 * b) - a) * (1 / (er - 1)) * 100;
altezza = str(h);
//rettangolo azzurro
pushStyle();
strokeWeight(4);
stroke(0, 187, 45);
point(77+(graph[y_start] * 4.4), 240-(h)*10);
//println(240-(h)*10);
//println((((graph[y_start] * 1)/10) * 44)+77);
point(77, 239);
popStyle();
pushStyle();
stroke(255);
strokeWeight(0.5);
line(79.21, 239, 329.1, 89 );
popStyle();
// disegno cilindro
pushStyle();
fill(255);
stroke(0);
rect(700, 39, 55, 200);
fill(0, 160, 254);
stroke(#FFFFFF, 1);
//rect(700, 39, 55, - (240-(h)*10));
if (h<15) rect(700, (240-(h)*10), 55, (h)*10);
popStyle();
}
// increment temporary circular buffer index and keep it in the array
y_start++;
if (y_start >= 500) {
y_start = 0;
}
}
}
void drawbottiglia() {
background(0);
lights();
translate(width/2+200, height/2 + 200, -200);
rotateZ(PI);
rotateY(ry);
shape(bottiglia);
fill(#776DF0);
ry += 0.02;
}