What exactly do you mean with
Is this because acqui() never returns?
?
The objective is to acquire data over a long time period (about 3 weeks) and turn the motor on/off for defined durations within a defined break in between leading to a on-off-on-off profile with 30 min-24h -30 min -24h as an example. Maybe I started the whole thing in a wrong way?
Could it be easier to create two subroutines; one acquiring the sensor data and a second one turning the motor on/off. In this case I even could adapt the data acquisition rate to lower frequency for motro off states and higher ones during motor on.
Would this be feasible for example using a code like this:
//pin-setup
void setup() {
pinMode(10, OUTPUT);
Serial.begin(9600);
establishContact();
}
//wait for contact...
void establishContact() {
while (Serial.available() <= 0) {
delay(300);
}
}
int F[4]= {0,0,0,0}; //sensor data
long unsigned time=0; //time axis
//---------------------- data ----------------------------//
void data(){ //acquisition
while(1){
delay(5000); //Data rate
F[0]=map(analogRead(0),1,1024,0,800); //sensor calibration
F[1]=map(analogRead(1),1,1024,0,800); //sensor calibration
F[2]=map(analogRead(2),1,1024,0,800); //sensor calibration
F[3]=map(analogRead(3),1,1024,0,800); //sensor calibration
Serial.print(millis()-time); //real time
Serial.print(" ");
Serial.print(F[0]);
Serial.print(" ");
Serial.print(F[1]);
Serial.print(" ");
Serial.print(F[2]);
Serial.print(" ");
Serial.println(F[3]);
}
}
//---------------------- motor function ----------------------------//
void aqui(long int t){ //acquisition
digitalWrite(10,1); //start the motor
int stopper=0;
while(1){
delay(50); //Data rate
F[0]=map(analogRead(0),1,1024,0,800); //sensor calibration
F[1]=map(analogRead(1),1,1024,0,800); //sensor calibration
F[2]=map(analogRead(2),1,1024,0,800); //sensor calibration
F[3]=map(analogRead(3),1,1024,0,800); //sensor calibration
Serial.print(millis()-time); //real time
Serial.print(" ");
Serial.print(F[0]);
Serial.print(" ");
Serial.print(F[1]);
Serial.print(" ");
Serial.print(F[2]);
Serial.print(" ");
Serial.println(F[3]);
if((millis()-time)>t){stopper++;}
if(stopper>2){break;}
}
digitalWrite(10,0); //stop the machine
}
//---------------------- String functionen ----------------------------//
char c=' ';
char str[10]; //String for parse
//empty string
void clstr(void){
for(int n=0;n!=10;n++){
str[n]=' ';
}
}
//read String
int strin(void){//check serial data acquisition
//only works if string available
delay(20); //time fram between port connections
if (Serial.available()){
clstr(); //empty string
int i=0;
while(Serial.available()){str[i]=Serial.read();
if(str[i]=='\n'){break;}
i++;
}
return(i);
}
else{return(0);}
}
long int readvalue(void){
return(atol(str)); //convert to int
}
int Z=0;
long int duration=0;
//---------------------- Hauptschleife ----------------------------//
void loop() {
time = millis();
data();
Z=strin();
//if input for duration was received
if(Z){
duration=readvalue();
aqui(duration*1000);
duration=0;
Z=0;
}
//if not input was received
if(!Z){digitalWrite(10,0);} //stop the machine
}
In this case I'm still lacking the possibility to start the motor by sending another number...