3 sensors - 2 analog, 1 digital. Serial Moniter output. ProcessingTry statement.

Hello everyone.

I would much much appreciate some help from you smart people! I use three sensors - 2 with analog output and 1 with digital.
The output from the 3 sensors goes through the Serial Monitor to processing. In processing I use a try-statement, to get processing to avoid the + or -, that the temperature sensor is programmed to make. When I only use my temperature sensor, it actually works. But when I use both my light sensor, my potentiometer and my temperature sensor in one Arduino sketch, Arduino wants to use the special temperature code on all three of the outputs. Leaving me with useless numbers from all three sensors.

This is my temperature code:

#define REF_PIN 2
void getCurrentTemp( int *sign, int *whole, int *fract);
char temp_string[10];

void setup(){
  Serial.begin(9600);
  // initialize DS18B20 datapin
  digitalWrite(REF_PIN, LOW);
  pinMode(REF_PIN, INPUT);      // sets the digital pin as input (logic 1)
  pinMode(15, INPUT);
}

void loop(){
  getCurrentTemp(temp_string);
  Serial.println(temp_string);
  delay(1000);
}

void OneWireReset (int Pin) // reset.  Should improve to act as a presence pulse
{
  digitalWrite (Pin, LOW);
  pinMode (Pin, OUTPUT);        // bring low for 500 us
  delayMicroseconds (500);
  pinMode (Pin, INPUT);
  delayMicroseconds (500);
}

void OneWireOutByte (int Pin, byte d) // output byte d (least sig bit first).
{
  byte n;

  for (n=8; n!=0; n--)
  {
    if ((d & 0x01) == 1)  // test least sig bit
    {
      digitalWrite (Pin, LOW);
      pinMode (Pin, OUTPUT);
      delayMicroseconds (5);
      pinMode (Pin, INPUT);
      delayMicroseconds (60);
    }
    else
    {
      digitalWrite (Pin, LOW);
      pinMode (Pin, OUTPUT);
      delayMicroseconds (60);
      pinMode (Pin, INPUT);
    }

    d = d>>1; // now the next bit is in the least sig bit position.
  }
}
byte OneWireInByte (int Pin) // read byte, least sig byte first
{
  byte d, n, b;

  for (n=0; n<8; n++)
  {
    digitalWrite (Pin, LOW);
    pinMode (Pin, OUTPUT);
    delayMicroseconds (5);
    pinMode (Pin, INPUT);
    delayMicroseconds (5);
    b = digitalRead (Pin);
    delayMicroseconds (50);
    d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
  }
  return (d);
}
void getCurrentTemp (char *temp)
{
  int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;

  OneWireReset (REF_PIN);
  OneWireOutByte (REF_PIN, 0xcc);
  OneWireOutByte (REF_PIN, 0x44); // perform temperature conversion, strong pullup for one sec

  OneWireReset (REF_PIN);
  OneWireOutByte (REF_PIN, 0xcc);
  OneWireOutByte (REF_PIN, 0xbe);

  LowByte = OneWireInByte (REF_PIN);
  HighByte = OneWireInByte (REF_PIN);
  TReading = (HighByte << 8) + LowByte;
  sign = TReading & 0x8000;  // test most sig bit
  if (sign) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  whole = Tc_100 / 100;  // separate off the whole and fractional portions
  fract = Tc_100 % 100;

 if (sign) {
  temp[0] = '+';
 } else {
  temp[0] = '-';
  }

  if (whole/100 == 0) {
    temp[1] = ' ';
  } else {
    temp[1] = whole/100+'0';
  }

  temp[2] = (whole-(whole/100)*100)/10 +'0' ;
  temp[3] = whole-(whole/10)*10 +'0';
  temp[4] = '.';
  temp[5] = fract/10 +'0';
  temp[6] = fract-(fract/10)*10 +'0';
  temp[7] = '\0';
}

This is my unsuccessful try to combine the temp code with the codes of my light sensor and potentiometer:

#define REF_PIN 2
void getCurrentTemp( int *sign, int *whole, int *fract);
char temp_string[10];
int light = 0;    // first analog sensor
int wind = 0;   // second analog sensor
int temp = 0;    // digital sensor
int inByte = 0;         // incoming serial byte

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect. 
  }

  establishContact();  // send a byte to establish contact until receiver responds 

  digitalWrite(REF_PIN, LOW);
  pinMode(REF_PIN, INPUT);      // sets the digital pin as input (logic 1)
  pinMode(15, INPUT);
}

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
 
    wind = analogRead(A1);                  //wind = speed . 
    wind = map(wind, 0, 1023, 0, 20);  
     delay(10);
     
    getCurrentTemp(temp_string);
    delay(10);
      
    light = analogRead(A2);
    light = map(light,0, 1023, 0, 255);     // light = cubeColor . ret ustabil
    //light = map(light,250, 500, 50, 100);
    //light = map(light,500, 750, 100, 150);
    //light = map(light,750, 1000, 150, 200);
    // light = map(light,1000, 1023, 200, 255);
       delay(10);
       
    // send sensor values:
    Serial.write(wind);
    Serial.write(temp_string);  
    Serial.write(light);   
    
    
  }
}

void OneWireReset (int Pin) // reset.  Should improve to act as a presence pulse
{
  digitalWrite (Pin, LOW);
  pinMode (Pin, OUTPUT);        // bring low for 500 us
  delayMicroseconds (500);
  pinMode (Pin, INPUT);
  delayMicroseconds (500);
}

void OneWireOutByte (int Pin, byte d) // output byte d (least sig bit first).
{
  byte n;

  for (n=8; n!=0; n--)
  {
    if ((d & 0x01) == 1)  // test least sig bit
    {
      digitalWrite (Pin, LOW);
      pinMode (Pin, OUTPUT);
      delayMicroseconds (5);
      pinMode (Pin, INPUT);
      delayMicroseconds (60);
    }
    else
    {
      digitalWrite (Pin, LOW);
      pinMode (Pin, OUTPUT);
      delayMicroseconds (60);
      pinMode (Pin, INPUT);
    }

    d = d>>1; // now the next bit is in the least sig bit position.
  }
}

byte OneWireInByte (int Pin) // read byte, least sig byte first
{
  byte d, n, b;

  for (n=0; n<8; n++)
  {
    digitalWrite (Pin, LOW);
    pinMode (Pin, OUTPUT);
    delayMicroseconds (5);
    pinMode (Pin, INPUT);
    delayMicroseconds (5);
    b = digitalRead (Pin);
    delayMicroseconds (50);
    d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
  }
  return (d);
}


void getCurrentTemp (char *temp)
{
  int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;

  OneWireReset (REF_PIN);
  OneWireOutByte (REF_PIN, 0xcc);
  OneWireOutByte (REF_PIN, 0x44); // perform temperature conversion, strong pullup for one sec

  OneWireReset (REF_PIN);
  OneWireOutByte (REF_PIN, 0xcc);
  OneWireOutByte (REF_PIN, 0xbe);

  LowByte = OneWireInByte (REF_PIN);
  HighByte = OneWireInByte (REF_PIN);
  TReading = (HighByte << 8) + LowByte;
  sign = TReading & 0x8000;  // test most sig bit
  if (sign) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  whole = Tc_100 / 100;  // separate off the whole and fractional portions
  fract = Tc_100 % 100;

 if (sign) {
  temp[0] = '-';
 } else {
  temp[0] = '+';
  }

  if (whole/100 == 0) {
    temp[1] = ' ';
  } else {
    temp[1] = whole/100+'0';
  }

  temp[2] = (whole-(whole/100)*100)/10 +'0' ;
  temp[3] = whole-(whole/10)*10 +'0';
  temp[4] = '.';
  temp[5] = fract/10 +'0';
  temp[6] = fract-(fract/10)*10 +'0';
  temp[7] = '\0';
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A');   // send a capital A
    delay(300);
  }
}

This is my Processing code without the nessesary try statement, that therefore doesn't work:

import processing.serial.*;

int speed;               // Speed of the rotating points = wind
int pointSize;           // size of the points = temp
int cubeColor;           // color of the cube = lys
           
Serial myPort;                       // The serial port
int[] serialInArray = new int[3];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
boolean firstContact = false;        // Whether we've heard from the microcontroller

int [] x = new int[100]; //I create three containers to my points
int [] y = new int[100];
int [] z = new int[100];

void setup() {
  size(600, 600, P3D);  // Stage size
  stroke(255);
  noFill();
//  strokeWeight(speed);
  smooth();
    
  String portName = Serial.list()[0];
  myPort = new Serial(this, "/dev/tty.usbmodem1d11", 9600);
}

void draw() {
  background(255);
  fill(cubeColor, 150-cubeColor, 255-cubeColor, 100);
 translate(width/2, height/2);

pushMatrix();
   // rotateY(radians(frameCount+speed));
   // rotateX(frameCount+speed);
    rotateY(10);
    box(300);
    popMatrix();
    lights();
   
     for(int i = 0; i<100; i++) {
  point (x[i], y[i], z[i]);
 //rotateY(frameCount); //Rotation af the points
// rotateX(frameCount- speed);
 //rotateZ(frameCount - pointSize);
}
  strokeWeight(pointSize);
  // stroke(255-cubeColor, 155-cubeColor, 200);
    stroke(255);

  for(int i = 0; i<100; i++) {   
 x[i] = int(random(-110, 110)); 
 y[i] = int(random(-110, 110)); 
 z[i] = int(random(-110, 110));
    //I create a forloop placing the particles inside the box. 
  }
  println(serialInArray); //print it out in the console
}

void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's an A,
  // clear the serial buffer and note that you've
  // had first contact from the microcontroller. 
  // Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 'A') { 
      myPort.clear();          // clear the serial port buffer
      firstContact = true;     // you've had first contact from the microcontroller
      myPort.write('A');       // ask for more
    } 
  } 
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;

    if (serialCount > 2 ) {
      speed = serialInArray[0];
      pointSize = serialInArray[1];
      cubeColor = serialInArray[2];
     

      // Send a capital A to request new sensor readings:
      myPort.write('A');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}

And this is the Processing try, that I want to add, but don't know where to correctly put:

float myFloat = 0.0;

void draw()
    try {
      myFloat = parseFloat(val);
    } catch (NullPointerException e) {
      myFloat = 0.0;
    }

I hope I didn't loose all from this long post. Any help would be much appreciated!!

Best
Ane

Why don't you send an identifier before each piece of data so that in Processing you can deal with each of them differently if required ? It would also help to keep the 2 systems synchronised as the Serial link can corrupt or lose data.

Even before tried with processing .Some time difficult to get data
Here i am trying to understand ur problem in better way. If you like to display 3 sensors - 2 analog, 1 digital. Serial Moniter output its better to go with Visual Basic. Easy to get data. I can share you the code for it. Which help you.

UKHeliBob:
Why don't you send an identifier before each piece of data so that in Processing you can deal with each of them differently if required ? It would also help to keep the 2 systems synchronised as the Serial link can corrupt or lose data.

That sounds like the perfect solution! I'm not the most experienced in Arduino/Processing, so would it be possible for you to concretize it a bit more? Thank you :slight_smile:

AMPS-N:
Even before tried with processing .Some time difficult to get data
Here i am trying to understand ur problem in better way. If you like to display 3 sensors - 2 analog, 1 digital. Serial Moniter output its better to go with Visual Basic. Easy to get data. I can share you the code for it. Which help you.

Hi :slight_smile: I'm not completely sure what that is, but I will much appreciate some code, that could explain it a bit more :slight_smile: Thank you.

so would it be possible for you to concretize it a bit more?

OUCH !

On the Arduino the idea would be something like this

    // send sensor values:
    Serial.print("W");
    Serial.print(wind);
    Serial.print(",T");
    Serial.print(temp_string);
    Serial.print(",L");
    Serial.println(light);

In Processing, which I know nothing about, you read the serial input into a variable, possibly an array, until you get a Carriage Return. Now you have the complete message which will be something like W11,T23,L123. Now you can split it at the commas and by using the first character of each piece decide which variable to put the number portion in.

As you are can decide what format to use to send the data you can change it to make it easier to use in Processing of course.

UKHeliBob:

so would it be possible for you to concretize it a bit more?

OUCH !

Haha :slight_smile:

In Processing, which I know nothing about, you read the serial input into a variable, possibly an array, until you get a Carriage Return. Now you have the complete message which will be something like W11,T23,L123. Now you can split it at the commas and by using the first character of each piece decide which variable to put the number portion in.

As you are can decide what format to use to send the data you can change it to make it easier to use in Processing of course.

Thank you for the code and your thoughts about it. I am actually already reading the values into an Array, and accessing these individually in Processing, so my problem might be more in Arduino, where I can't get the temperature-minded code to work only on the temperature related parts. I get all of the codes, from all of my sensors included. Without the temperature sensor it works fine with identifying the individual values in Processing. But the complicated code for the digital ridden temperature sensor influences it all. If that makes sense :slight_smile: So I need a way to get Arduino only to apply the temperature-related code onto the temperature values.

:slight_smile:

What do you see in temp_string on the Arduino before you send it ?