We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I have one problem with my code. When I press a button to draw lines from the values obtained from the Serial port, some random lines appeared. Here is the code :
import controlP5.*;
import processing.serial.*;
ControlP5 controlP5;
Serial myPort;
//storage of the sensor values
float [] sen1= new float [999999];
float [] sen2= new float [999999];
float [] sen3= new float [999999];
float [] sen4= new float [999999];
float [] sen5= new float [999999];
float [] sen6= new float [999999];
//storage of the values to plot the lines
float [] plot_sen1= new float [999999];
float [] plot_sen2= new float [999999];
float [] plot_sen3= new float [999999];
float [] plot_sen4= new float [999999];
float [] plot_sen5= new float [999999];
float [] plot_sen6= new float [999999];
// strings where the values will be storaged in the text file
String [] string_sen1= new String [999999];
String [] string_sen2= new String [999999];
String [] string_sen3= new String [999999];
String [] string_sen4= new String [999999];
String [] string_sen5= new String [999999];
String [] string_sen6= new String [999999];
String [] save_string= new String [999999];
String filename,name;
//String [] string_sen1;
PImage img; // import the image
float s1,s2,s3,s4,s5,s6,s7,s8,s9,s10;
int value=0;
int xPos=30;
//variables de color
color blue= #2500FF;
color green=#00FF3D;
color yellow=#F6FF00;
color orange=#FFAE58;
color red=#FF0303;
color x1,x2,x3,x4,x5,x6;
int button1=0;
int button2=0;
int button3=0;
int save=0;
int count;
float residuo1;
float residuo2;
float residuo3;
float save_residuo;
boolean sens1= false;
boolean sens2= false;
boolean sens3= false;
boolean sens4= false;
boolean sens5= false;
boolean sens6= false;
boolean sens7= false;
boolean sens8= false;
boolean sens9= false;
boolean sens10=false;
PFont myFont,title,font;
void setup(){
background(#0B5189);
size(1024,900);
smooth();
controlP5= new ControlP5(this);
controlP5.addBang(" Start ",610,20,100,100);
controlP5.addBang(" Reset ",720,20,100,100);
//controlP5.addBang(" Graph3 ",830,20,100,100);
controlP5.addBang(" Save ",830,20,100,100);
//change the color of the buttons
controlP5.setColorLabel(#E8BCFF);
controlP5.setColorForeground(#E8BCFF);
//controlP5.addTextfield("Name of the document").setPosition(760,350).setSize(200,40).setAutoClear(false);
println(Serial.list());
myPort=new Serial(this,Serial.list()[1],115200);
myPort.bufferUntil('\n');
fill(255);
myFont= createFont("arial",20);
title= createFont("arial",16);
font=createFont("arial", 20);
//objetos botones
}
void draw()
{
beginShape();
fill(x1);
stroke(x1);
vertex(010,100);
vertex(050,100);
vertex(185,170);
vertex(130,180);
endShape();
beginShape();
fill(x2);
stroke(x2);
vertex(80,110);
vertex(130,110);
vertex(235,165);
vertex(200,175);
endShape();
beginShape();
fill(x3);
stroke(x3);
vertex(160,120);
vertex(210,110);
vertex(280,150);
vertex(250,160);
endShape();
//1//
}
void serialEvent ( Serial myPort){
String inString= new String(myPort.readBytesUntil('\n'));
if(inString !=null){
String q[]= splitTokens(inString,",");
if (q.length >0){
s1= float(q[0]);
s2= float(q[1]);
s3= float(q[2]);
s4= float(q[3]);
s5= float(q[4]);
s6= float(q[5]);
sen1[count]=s1;
sen2[count]=s2;
sen3[count]=s3;
sen4[count]=s4;
sen5[count]=s5;
sen6[count]=s6;
//map the values to plot the line
plot_sen1[count]= map(s1,0,1023,0,400);
plot_sen2[count]= map(s2,0,1023,0,400);
plot_sen3[count]= map(s3,0,1023,0,400);
plot_sen4[count]= map(s4,0,1023,0,400);
plot_sen5[count]= map(s5,0,1023,0,400);
plot_sen6[count]= map(s6,0,1023,0,400);
//sensor 6
if (sens1==true){
stroke(0);
line(xPos-1,880-plot_sen1[count-1],xPos,880-plot_sen1[count]);
stroke(#E800B6);
line(xPos-1,880-plot_sen2[count-1],xPos,880-plot_sen2[count]);
stroke(#03FF58) ;
line(xPos-1,880-plot_sen3[count-1],xPos,880-plot_sen3[count]);
}
if (sens2==true){
stroke(#0B5189);
line(xPos-1,880-plot_sen1[count-1],xPos,880-plot_sen1[count]);
line(xPos-1,880-plot_sen2[count-1],xPos,880-plot_sen2[count]);
line(xPos-1,880-plot_sen3[count-1],xPos,880-plot_sen3[count]);
}
if(sen1[count] > 0 && sen1[count] <= 204){
x1=blue;
}
if(sen1[count] > 204 && sen1[count] <=408){
x1=green;
}
if(sen1[count] >408 && sen1[count] <=612) {
x1=yellow;
}
if(sen1[count] >612 && sen1[count] <=816) {
x1=orange;
}
if(sen1[count] > 816 ) {
x1=red;
}
//sensor2
if(sen2[count] > 0 && sen2[count] <= 204){
x2=blue;
}
if(sen2[count] > 204 && sen2[count] <=408){
x2=green;
}
if(sen2[count] >408 && sen2[count] <=612) {
x2=yellow;
}
if(sen2[count] >612 && sen2[count] <=816) {
x2=orange;
}
if(sen2[count] > 816 ) {
x2=red;
}
//sen3
if(sen3[count] > 0 && sen3[count] <= 204){
x3=blue;
}
if(sen3[count] > 204 && sen3[count] <=408){
x3=green;
}
if(sen3[count] >408 && sen3[count] <=612) {
x3=yellow;
}
if(sen3[count] >612 && sen3[count] <=816) {
x3=orange;
}
if(sen3[count] > 816 ) {
x3=red;
}
//sen 4
if(sen4[count] > 0 && sen4[count] <= 204){
x4=blue;
}
if(sen4[count] > 204 && sen4[count] <=408){
x4=green;
}
if(sen4[count] >408 && sen4[count] <=612) {
x4=yellow;
}
if(sen4[count] >612 && sen4[count] <=816) {
x4=orange;
}
if(sen4[count] > 816 ) {
x4=red;
}
//sen5
if(sen5[count] > 0 && sen5[count] <= 204){
x5=blue;
}
if(sen5[count] > 204 && sen5[count] <=408){
x5=green;
}
if(sen5[count] >408 && sen5[count] <=612) {
x5=yellow;
}
if(sen5[count] >612 && sen5[count] <=816) {
x5=orange;
}
if(sen5[count] > 816 ) {
x5=red;
}
//sen6
if(sen6[count] > 0 && sen6[count] <= 204){
x6=blue;
}
if(sen6[count] > 204 && sen6[count] <=408){
x6=green;
}
if(sen6[count] >408 && sen6[count] <=612) {
x6=yellow;
}
if(sen6[count] >612 && sen6[count] <=816) {
x6=orange;
}
if(sen6[count] > 816 ) {
x6=red;
}
}
}
count++;
if (xPos >=1015) {
xPos=30;
background(#0B5189);
}
else
{
xPos++;
}
}
void controlEvent(ControlEvent theEvent){
if (theEvent.isController()){
if (theEvent.controller().name()==" Start "){
button1=button1+1;
residuo1=button1 % 2;
int count=1;
if(residuo1== 0){
sens1=false;
println("par");
}
else
{
sens1=true;
println("impar");
}
}
if (theEvent.controller().name()==" Reset "){
button2=button2+1;
residuo2=button2 % 2;
if(residuo2== 0){
sens2=false;
sens1=false;
println("par");
}
else
{
sens2=true;
println("impar");
}
}
}
}
Answers
http://forum.processing.org/two/discussion/10031/serial-out-to-txt
@GoToLoop , could you be more specific ? Thanks!
you could just read the thread gotoloop posted and find your solution therein...
;-)