Problem working with multiple sensors

edited November 2013 in Arduino

Hi Everybody. I was hoping you could provide me with some assistance. I am fairly new to both the Arduino and Processing environments, but am trying to develop some code for what I think is a fairly basic project idea. However, I am having a little bit of difficulty.

What I am trying to do is take readings from two strain gauge sensors, and use these strain gauges to change to the colour (from green to red corresponding to the level of strain applied to the sensors) of two corresponding rectangles in Processing. I have the strain gauges wired up to the arduino in analogue ports 0 and 1. I have developed some code in arduino, and when the code is run in the serial monitor, I am receiving data from both strain gauges.

However, when I run the processing code I have developed, one of the rectangles (the one receiving sensor data from analogue pin A0) changes colour as I would like it to. But the other rectangle doesn't change colour at all. I am not sure why, as it looks like I am doing everything ok. I am not sure if this is a problem with my arduino code or my processing code.

I have attached below both my arduino and processing code. Any help with this matter would be greatly appreciated. I know it is only a very simple program, but it is becoming very frustrating not being able to sort it.

Thanks in advance. G.

Arduino Code

int gauge0;
int gauge1;

void setup()
{
Serial.begin(9600);
}

void loop()
{
gauge0=analogRead(A0);
gauge1=analogRead(A1);

Serial.print(gauge0);
Serial.print(", ");
delay(10); // I have put these delays in to help the multiplexer in the onboard microcontroller of the arduino
Serial.println(gauge1);
delay(10);
}

Processing Code

import processing.serial.*;

int gauge0=0;
int gauge1=1;

Serial myPort;

void setup()
{
size (800, 500);
myPort = new Serial(this, "COMPORT", 9600);
myPort.bufferUntil('\n');
}

void draw()
{
pushMatrix();
translate(300, 260);
fill(gauge0, (100-gauge0), 0); // fills colour so that the resting value is green, and max strain value is red
rect(100, 100, 100, 100);
popMatrix();

pushMatrix();
translate(400, 260);
fill(gauge1, (100-gauge1), 0);
rect(100, 100, 100, 100);
popMatrix();
}

void serialEvent(Serial myPort)
{
String inString = myPort.readStringUntil('\n');

if (inString!=null); // this area splits the arduino inputs such that they can be received seperately
   {
     inString=trim(inString);
      int[] sensors= int (split(inString, ","));

      if (sensors.length>=2)
         {
          gauge0=sensors[0];
          gauge1=sensors[1];
        }
   }
}

Answers

  • Answer ✓

    in arduino code.
    Serial.print(", "); try Serial.print(",");

  • Hi Camperos. Thank you for your reply. I am currently away this weekend, but will check out your suggestion tomorrow. I've been an idiot - i didn't realise I had put a space after the comma. Thanks for your help.

  • Thanks for your help Camperos. Your suggestion sorted out my problem. Regards G.

Sign In or Register to comment.