Whereas this is a likely easy to solve problem I dont really get where the mistake is.
Wanting to do an Uniformly accelerated linear motion simulator with a HC-SR04. I bulit a 90cm brigde where a ball will start falling. The HC-SR04 will detect the 7cm left wall (and time stamp) until the ball drops there so it will detect a 1 or 2 cm distance(2nd time stamp and red Led turns). Then it's supposed to stop the timer and do the formula.
Or well, that's what it's supposed to do
Here is my code (ignore the spanish notes):
unsigned long tiempo =0;
unsigned long tiempo2 =0;
int segundos = 0;
int diferencia = 1000;
const double dist = 90;
#define Pecho 6
#define Ptrig 7
#define Micro 8
long duracion, distancia;
void setup() {
Serial.begin(9600);
pinMode(Pecho, INPUT); // define el pin 6 como entrada (echo)
pinMode(Ptrig, OUTPUT); // define el pin 7 como salida (triger)
pinMode(13, 1); // Define el pin 13 como salida
pinMode(8, 1);
}
void loop() {
tiempo = millis();
if(tiempo >= (tiempo2+diferencia))
{
tiempo2 = tiempo;
segundos = tiempo/1000;
Serial.println ("Segundos trancurridos:");
Serial.println (segundos);
}
digitalWrite(Ptrig, LOW);
delayMicroseconds(2);
digitalWrite(Ptrig, HIGH); // genera el pulso de triger por 10ms
delayMicroseconds(10);
digitalWrite(Ptrig, LOW);
duracion = pulseIn(Pecho, HIGH);
distancia = (duracion/2) / 29; // calcula la distancia en centimetros
while (distancia == 7 || distancia == 8){} // si la distancia es 7 u 8
double timeDrop1 = millis(); // captura el tiempo
while (distancia <= 7 && distancia >= 1) {} // si la distancia es menor a 7cm y mayor a 1cm
double timeDrop2 = millis(); //captura el tiempo
while (distancia <= 7 && distancia >= 1) {}
digitalWrite(13, 1); // en alto el pin 13 si la distancia es menor a 10cm
digitalWrite(8,1);
}
//step 2
{
timeDrop = millis() - timeDrop2;
double dist/Acc = 0 * timeDrop + 1/2 * timeDrop;
Serial.println();
Serial.println();
Serial.print("Se ha calculado la aceleración en base de ");
Serial.print(timeDrop);
Serial.println(" seg");
Serial.print("con una distancia de ");
Serial.print(dist);
Serial.println(" cm");
Serial.print("y es ");
Serial.print(Acc);
Serial.println(" m/s^2");
}
And the error
Prueba_MRUV:58:3: error: expected unqualified-id before '{' token
{
^
exit status 1
expected unqualified-id before '{' token
The } marked aaaa below is the closing buddy of the one that starts loop(), so everything after that, from the bbbbb { down is outside the sketch. (edit: Or I should say, in the sketch but outside of setup() or loop() and isn't a function in its own right.(
while (distancia <= 7 && distancia >= 1) {}
digitalWrite(13, 1); // en alto el pin 13 si la distancia es menor a 10cm
digitalWrite(8,1);
} //<<<<<<<<<<<<<< aaaaaa
//step 2
{ //<<<<<<<<<<<<<< bbbb
timeDrop = millis() - timeDrop2;
If step2 is supposed to be inside loop() move he aaaaa one right to the end:
Serial.print(Acc);
Serial.println(" m/s^2");
}
} // new aaaaaaa
At that point, you will get other errors like timeDrop not being declared. Maybe everything from bbbbb down isn't meant to be there? (If you comment out from bbbb down, it compiles.)
All C/C++ code must be in a function. (There may be exceptions that don't apply here.) Your Step 2 code has brackets but is not in any function. The loop() function ended with the curly bracket just ahead of the Step 2 comment.
12Stepper:
At that point, you will get other errors like timeDrop not being declared. Maybe everything from bbbbb down isn't meant to be there? (If you comment out from bbbb down, it compiles.)
As you mention the "not declared in this scope" started appearing
C:\Users\USER\Desktop\Prueba_MRUV\Prueba_MRUV.ino: In function 'void loop()':
Prueba_MRUV:60:5: error: 'timeDrop' was not declared in this scope
timeDrop = millis() - timeDrop2;
^
Prueba_MRUV:62:16: error: expected initializer before '/' token
double dist/Acc = 0 * timeDrop + 1/2 * timeDrop;
^
Prueba_MRUV:73:18: error: 'Acc' was not declared in this scope
Serial.print(Acc);
^
exit status 1
'timeDrop' was not declared in this scope
The point is that, when the ball passes afront the sensor the time has stop. Then, the monitor should show the "Se ha calculado la aceleración en base de " message and everything after it. For this to work the "Acc" variable should show up from the formula. I really don't know what to do for this to happen. Errors just keep spaming
I changed the "part 2" so i would make more sense. However I still get 1 more error.
the code:
//step 2
{
timeDrop = millis() - timeDrop2;
double Acc = dist/(timeDrop + 1/2 * timeDrop)
Serial.println();
Serial.println();
Serial.print("Se ha calculado la aceleración en base de ");
Serial.print(timeDrop);
Serial.println(" seg");
Serial.print("con una distancia de ");
Serial.print(dist);
Serial.println(" cm");
Serial.print("y es ");
Serial.print(Acc);
Serial.println(" m/s^2");
}
}
the error:
C:\Users\USER\Desktop\Prueba_MRUV\Prueba_MRUV.ino: In function 'void loop()':
Prueba_MRUV:60:5: error: 'timeDrop' was not declared in this scope
timeDrop = millis() - timeDrop2;
^
exit status 1
'timeDrop' was not declared in this scope
Is anything wrong with the "timeDrop = millis() - timeDrop2;" part? What should I put then? Thanks in advance.
Look at the very top of your sketch, where you have lines like:
unsigned long tiempo =0;
unsigned long tiempo2 =0;
Those lines declare the variables tiempo and tiempo2, and in this case also give them starter values of 0.
You need similar declarations for timeDrop and timeDrop2, otherwise they can't be used. The error is telling you exactly that: they're not declared.
The point is that, when the ball passes afront the sensor the time has stop.
What you want it to do, has nothing to do with the fact that you can't get it to compile. You need to get it to compile before you can worry about it working properly or not.
As indicated by the others, the last } above indicates the end of loop(); anything after that is not in a function and hence the compile error. Remove both the } before //step 2 and the { after //step 2 and the one error will be gone.
Next error will be about the timeDrop variable
timeDrop = millis() - timeDrop2;
Next error will be about this line; no idea what you're trying to achieve so I can't fix it.
This would be the acceleration formula in Uniformly Accelerated Linear Motion (Distannce=Initial VelocityTime+1/2Acceleration*time^2). I change it to this
About the first thing, not pretty sure if I made thing ok. timeDrop supposed to be the time between 1st and second "while".
Code that compiles but does not work:
unsigned long tiempo =0;
unsigned long tiempo2 =0;
int segundos = 0;
int diferencia = 1000;
const double dist = 90;
#define Pecho 6
#define Ptrig 7
#define Micro 8
long duracion, distancia;
void setup() {
Serial.begin(9600);
pinMode(Pecho, INPUT); // define el pin 6 como entrada (echo)
pinMode(Ptrig, OUTPUT); // define el pin 7 como salida (triger)
pinMode(13, 1); // Define el pin 13 como salida
pinMode(8, 1);
}
void loop() {
tiempo = millis();
if(tiempo >= (tiempo2+diferencia))
{
tiempo2 = tiempo;
segundos = tiempo/1000;
Serial.println ("Segundos trancurridos:");
Serial.println (segundos);
}
digitalWrite(Ptrig, LOW);
delayMicroseconds(2);
digitalWrite(Ptrig, HIGH); // genera el pulso de triger por 10ms
delayMicroseconds(10);
digitalWrite(Ptrig, LOW);
duracion = pulseIn(Pecho, HIGH);
distancia = (duracion/2) / 29; // calcula la distancia en centimetros
while (distancia == 7 || distancia == 8){} // si la distancia es 7 u 8
double timeDrop1 = millis(); // captura el tiempo
while (distancia <= 7 && distancia >= 1) {} // si la distancia es menor a 7cm y mayor a 1cm
double timeDrop2 = millis(); //captura el tiempo
while (distancia <= 7 && distancia >= 1) {}
digitalWrite(13, 1); // en alto el pin 13 si la distancia es menor a 10cm
digitalWrite(8,1);
double timeDrop = millis() - timeDrop2;
double Acc = dist/(0 * timeDrop + 1/2 * pow(timeDrop, 2));
//step 2
delay(600);
Serial.println();
Serial.println();
Serial.print("Se ha calculado la aceleración en base de ");
Serial.print(timeDrop);
Serial.println(" seg");
Serial.print("con una distancia de ");
Serial.print(dist);
Serial.println(" cm");
Serial.print("y es ");
Serial.print(Acc);
Serial.println(" m/s^2");
}
Multiplying something by 1/2 is not useful either. In C integer arithmetic, 1/2 is zero so there is an additional multiplication by zero unless timeDrop is declared float or similar. I have not checked to see if the function pow(...) would work.