Need advice Haven't done C in a long time.

Hi there,
So, the thing here is that I am trying to down the reading time (current reading time is about 256 readings for 4 seconds) and increase the data points (array???) receive with the program. The Script is being used to obtain data from a pressure sensor from the arduino. A group of my friends are trying to build a pressure chambers to get readings but we’re all used to basic coding ><. So I was wondering if I would be able to get some help :)? This is the script we have going on right now.

int analogPin = 1; // Pin A0 is used to connect the pressure transducer
int sensorValue; // Value of sensor reading ranging from 0 to 1023
int buttonPin = 2; // Pin D2 is used to connect the switch to start input reading
float voltageMax=5; // 5V
float pressureMax=1000; // 1000 milipsig – 5V
int sizeOfArray = 255; // Use 2arrays of int
int arrayms[255]; // array to store time
int arrayVoltage[255]; // array to store voltage
int a=0;
int b=0;
long startTime;

void setup() {
pinMode(analogPin, INPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}

//================Main Loop=====================================================================//

{
void loop() {
// Hold button when testing
while((a<=sizeOfArray))// && (digitalRead(buttonPin)))
{
if (a==0){
startTime=millis(); // start time counter for the first iteration only.
}
sensorValue = analogRead(analogPin); // get input reading from analogPin / pressure sensor
arrayVoltage[a] = sensorValue; // Put data in voltage array
arrayms[a] = millis()-startTime; // record time
a++;
delay(16); //delay based on 4 sec read time (4/256 = 15.6 ms)
}

//-----------------Send array data to Gobetwino-----------

if ((a>=50))// && (digitalRead(buttonPin))) // only allow sending to computer if there is more than 50 samples read; require user to press button.
{
//sizeOfArray = a;

while(b<=sizeOfArray)
{
Serial.print("#S|PRESSURE|["); //use command LGFIL on Gobetwino, name it PRESSURE, saved data under data.txt
Serial.print(arrayms**); //print time stamp**
** Serial.print("\t");**
Serial.print(arrayVoltage__(voltageMax/1023),4); // print voltage in decimal number__
** Serial.print("\t");**
Serial.print(arrayVoltage__
(pressureMax/1023),4); // print Pressure in decimal number
** Serial.println("]#");
b++;
// The result would be like: ; ;
}
}
}**
Any advice would be appreciated :), time is of the essence for this project.
Thanks very much! :slight_smile:__

Board?

duelaminove atmega328

Regardless of anything else you do, your Sketch is writing past the end of both arrays. You need to increase the size by one…

int arrayms[256]; // array to store time
int arrayVoltage[256]; // array to store voltage

So, the thing here is that I am trying to down the reading time (current reading time is about 256 readings for 4 seconds)

void loop() {
// irrelevant code snipped
a++;
delay(16); //delay based on 4 sec read time (4/256 = 15.6 ms)
}

Use a value less than 16.

void loop() {
startTime=millis(); // start time counter for the first iteration only.
// Hold button when testing
while((a<=sizeOfArray))// && (digitalRead(buttonPin)))
{
sensorValue = analogRead(analogPin); // get input reading from analogPin / pressure sensor

Move the startTime initialization outside of the while loop.

increase the data points (array???) receive with the program.

const int sizeOfArray = 511; // Use 2arrays of int
int arrayms[sizeOfArray+1]; // array to store time
int arrayVoltage[sizeOfArray+1]; // array to store voltage

Thanks! My friend has the board so I can’t currently check it out, but I did do the adjustments (made sense). But

const int sizeOfArray = 511; // Use 2arrays of int
int arrayms[sizeOfArray+1]; // array to store time
int arrayVoltage[sizeOfArray+1]; // array to store voltage

for that part, is there a need to make it constant? I’m just curious.

But Thanks again! :slight_smile:

If you want to use sizeOfArray when declaring the arrays then you have to make it a constant. It really should be a "static const"...

static const int sizeOfArray = 511; // Use 2arrays of int