How to continuously get data from my sensor

Hi,
I want my code to work for in a way that the second sensor in the coding works for 7 seconds. This works now, but only prints out one value every 7 seconds. I want it to print all values from 0-7 seconds and then stay idle for another 7 seconds until the loop restarts again.This is then send to matlab where a signal from serial FT232 is used and plot a graph. Any help on this would be appreciated.

// int sensors 
#include <SoftwareSerial.h>
#define rxPin 0
#define txPin 1
const int sensorA1=analogRead(A1);
const int sensorA3=analogRead(A5);
int relay_pin=digitalRead(13);
int Interval;
SoftwareSerial mySerial(rxPin,txPin);// setup new serial ports
void setup() 
{
 Serial.begin(9600);// setting data rate for analog and digital input
 mySerial.begin(9600);// setting data rate for serial input
 pinMode (13,OUTPUT);//Defining ports as output
 pinMode (rxPin,INPUT);
 pinMode (txPin,OUTPUT);
} 
void loop()
{
 //Sensor A1 read and print
 Serial.print("Sensor Value (A1): ");
 Serial.print(analogRead(A1));
 float voltageA1 = (analogRead(A1)*5.0)/1024.0;// scaling towards 0-5v for analog read
 Serial.print(" Volts:(A1) ");
 Serial.print(voltageA1);  
 float pressure_pascalA1 = (0.505*((float)voltageA1-1))*1000000.0;// conversion voltage to pascal
 float pressure_barA1 = pressure_pascalA1/10e5;
 Serial.print(" Pressure before solenoid (A1) = ");
 Serial.print(pressure_barA1);//plotting data
 Serial.println(" bars");
 if (pressure_barA1<=0.32)// Pressure read when pressure below specific value.
{
 digitalWrite(13, HIGH);
 Serial.print("Sensor Value (A5): ");// Sensor A3 read and print, triggering of the second sensor for reading value.
 Serial.print(analogRead(A5));
 float voltageA5 = (analogRead(A5)*5.0)/1024.0;
 Serial.print(" Volts:(A5) ");
 Serial.print(voltageA5); 
 float pressure_pascalA5 = (0.505*((float)voltageA5-1))*1000000.0;
 float pressure_barA5 = pressure_pascalA5/10e5;
 Serial.print(" Pressure at chamber (A5) = ");
 Serial.print(pressure_barA5);// plotting data
 Serial.println(" bars");
 Serial.print("Waiting for 7 seconds");
} else 
 digitalWrite(13, LOW);
 delay (7000);
}
// int sensors 
#include <SoftwareSerial.h>
#define rxPin 0
#define txPin 1
const int sensorA1=analogRead(A1);
const int sensorA3=analogRead(A5);
int relay_pin=digitalRead(13);
int Interval;
SoftwareSerial mySerial(rxPin,txPin);// setup new serial ports
void setup() 
{
 Serial.begin(9600);// setting data rate for analog and digital input
 mySerial.begin(9600);// setting data rate for serial input
 pinMode (13,OUTPUT);//Defining ports as output
 pinMode (rxPin,INPUT);
 pinMode (txPin,OUTPUT);
} 
void loop()
{
 //Sensor A1 read and print
 Serial.print("Sensor Value (A1): ");
 Serial.print(analogRead(A1));
 float voltageA1 = (analogRead(A1)*5.0)/1024.0;// scaling towards 0-5v for analog read
 Serial.print(" Volts:(A1) ");
 Serial.print(voltageA1);  
 float pressure_pascalA1 = (0.505*((float)voltageA1-1))*1000000.0;// conversion voltage to pascal
 float pressure_barA1 = pressure_pascalA1/10e5;
 Serial.print(" Pressure before solenoid (A1) = ");
 Serial.print(pressure_barA1);//plotting data
 Serial.println(" bars");
 if (pressure_barA1<=0.32)// Pressure read when pressure below specific value.
{
 digitalWrite(13, HIGH);
 Serial.print("Sensor Value (A5): ");// Sensor A3 read and print, triggering of the second sensor for reading value.
 Serial.print(analogRead(A5));
 float voltageA5 = (analogRead(A5)*5.0)/1024.0;
 Serial.print(" Volts:(A5) ");
 Serial.print(voltageA5); 
 float pressure_pascalA5 = (0.505*((float)voltageA5-1))*1000000.0;
 float pressure_barA5 = pressure_pascalA5/10e5;
 Serial.print(" Pressure at chamber (A5) = ");
 Serial.print(pressure_barA5);// plotting data
 Serial.println(" bars");
 Serial.print("Waiting for 7 seconds");
}  
 digitalWrite(13, LOW);
 delay (7000);
}

may be one way. Using millis() would be a better way.

maybe if you put your code in a for argument

  for (int i = 0; i <= 7; i++) {
 
 you code here

    delay(1000);
  }
delay (7000);

i

Yep.

delay() is a crude/ugly way to get things done on time.
Try to get your head around millis() timing.
The BlinkWithoutDelay example in the IDE will help you understand the concept.
Leo..

There is probably nothing actually wrong with using delay - it is merely trivia that gets up the nose of the purists, thereby missing the real issues - and, while you don't say which Arduino you are using, you can be sure there is a lot wrong with having software serial on the hardware serial pins 0,1.

This might even be the only problem you actually have, and I suspect you can consider yourself lucky that you got any result at all.

Now I don't know anything about analogue connections but I bet there is something suss here.

const int sensorA1=analogRead(A1);
const int sensorA3=analogRead(A5);

and, if the latter works, it could be that both of them are nonsense.

Further, it seems that the software serial serves no purpose other than to cause grief, if it can. You call it but don't use it. As I understand it, all you are doing is reading a couple of analogue sensors and sending the data to the serial monitor.
If this is true, you have a swag of redundant code, and the comments attached to your serial setup commands are utter nonsense.

I also understand that you want to

  1. collect eight data at one second intervals
  2. then send same in a rush
  3. then stay idle for seven seconds
  4. then go back to 1.

I'm betting that is not what you really want, but that's what I think you are saying, and I don't dare ask why you would want to do that.

Whatever you think you want, I'm guessing that it would be a lot simpler if you just collect the data at one second intervals, delay(1000);, and send it up the line immediately. As a start your code might look like this

// int sensors

const int sensorA1=analogRead(A1);
const int sensorA3=analogRead(A5);
int relay_pin=digitalRead(13);

void setup()
{
Serial.begin(9600);
pinMode (13,OUTPUT);//Defining ports as output

}
void loop()

But probably still needs improvement

At the moment, all you are doing is taking a single set of readings at seven second intervals

Oh, I just realized I used my old code to paste it here. So basically I was trying to use the serial pins where the laser gauge was supposed to be reading, but the tech said the hardware wouldn't respond to Arduino uno and has to use FT232. I forgot to clear up the upper bit of the code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.