Hi, I'm doing a mini-project for college where I have to calculate the velocity and acceleration of a small ball when dropped by a bar moving at different angles. I have written this code, but when I compile it nothing works: the bar does not move and the sensors do not calculate anything. Can anybody help me?
Here I attach the code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Servo.h>
double servo2;
double vel;
double acc;
double t1;
double t2;
double t3;
double vel1;
double t4;
double vel2;
double vel3;
boolean b_s1;
boolean b_s2;
boolean b_s3;
boolean b_s4;
LiquidCrystal_I2C lcd(0x27,16,2);
unsigned long task_time_ms=0;
Servo servo_4;
Servo servo_5;
unsigned long time_timer=millis();
void serial() {
if((millis()-task_time_ms)>=2000){
task_time_ms=millis();
Serial.print(String("Velocitat: "));
Serial.println(vel);
Serial.print(String("Acceleracio: "));
Serial.println(acc);
}
}
void LCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(String("Vel"));
lcd.setCursor(8, 0);
lcd.print(String("Acc"));
lcd.setCursor(0, 1);
lcd.print(vel);
lcd.setCursor(8, 1);
lcd.print(acc);
}
void servo() {
for (servo2 = 72; servo2 >= 60; servo2=servo2-2) {
servo_4.write(servo2);
delay(100);
}
servo_5.write(45);
delay(2000);
servo_5.write(0);
delay(100);
for (servo2 = 60; servo2 <= 72; servo2=servo2+2) {
servo_4.write(servo2);
delay(100);
}
delay(3000);
for (servo2 = 72; servo2 >= 52; servo2=servo2-2) {
servo_4.write(servo2);
delay(100);
}
servo_5.write(45);
delay(2000);
servo_5.write(0);
delay(100);
for (servo2 = 52; servo2 <= 72; servo2=servo2+2) {
servo_4.write(servo2);
delay(100);
}
delay(3000);
for (servo2 = 72; servo2 >= 44; servo2=servo2-2) {
servo_4.write(servo2);
delay(100);
}
servo_5.write(45);
delay(2000);
servo_5.write(0);
delay(100);
for (servo2 = 44; servo2 <= 72; servo2=servo2+2) {
servo_4.write(servo2);
delay(100);
}
}
void sensors() {
b_s1 = digitalRead(8);
b_s2 = digitalRead(9);
b_s3 = digitalRead(10);
b_s4 = digitalRead(11);
if ((!b_s1)) {
time_timer=millis();
t1 = millis();
t1 = (t1 / 1000);
}
if ((!b_s2)) {
t2 = millis();
t2 = (t2 / 1000);
vel1 = (0.26 / ((t2 - t1)));
}
if ((!b_s3)) {
t3 = millis();
t3 = (t3 / 1000);
vel2 = (0.5 / ((t3 - t2)));
}
if ((!b_s4)) {
t4 = millis();
t4 = (t4 / 1000);
vel3 = (0.26 / ((t4 - t3)));
vel = (((vel1 + ((vel2 + vel3)))) / 3);
acc = (vel / ((t4 - t1)));
}
}
void setup()
{
lcd.init();
lcd.noCursor();
lcd.backlight();
Serial.begin(9600); Serial.flush(); while(Serial.available()>0)Serial.read();
servo_4.attach(4);
servo_5.attach(5);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
b_s1 = true;
b_s2 = true;
b_s3 = true;
b_s4 = true;
t1 = 0;
t2 = 0;
t3 = 0;
t4 = 0;
vel3 = 0;
vel = 0;
acc = 0;
servo2 = 72;
}
void loop()
{
sensors();
LCD();
serial();
servo();
LarryD
August 28, 2021, 11:24pm
2
Place Serial prints at strategic locations to make sure you values are what you think they should be.
Hum, Serial.print(String("Velocitat: ")); this is odd, why not Serial.print("Velocitat: "); or Serial.print("Velocitat: \n"); ?
BTW delay() suspends all program execution for that amount of time, not recommended.
While not looking at what’s happening, I suspect the delays are blocking your code from doing anything meaningful.
This is probably one of those rare cases, where I’d suggest using interrupts to detect when your balls drop.
Overall, you may find other benefits in millis() timing.
1 Like
So how do I write the code without delays? Sorry but I’m kind of new to all this
Thanks for responding, I will fix that but, ho do I fix the the code without all the delays?
Koepel
August 28, 2021, 11:42pm
6
Can you explain what the sequence is that the servo motors should perform ? and how often should they do that ? is that sequence triggered by something ?
When that sequence is in a table, then millis() can be used to walk through that table.
It’s a substantial task…
You need to think of your code as ‘event driven’ rather than linear/sequential.
There are MANY threads on here about learning and implementing millis timing, but you need to take on the load !
LarryD
August 28, 2021, 11:47pm
9
See BWD :
Tutorial on code blocking.
Attached is:
LEDsequenceDelay.ino
LEDsequenceMillis.ino
LEDsequenceDelayFlowChart.jpg
LEDsequenceMillisFlowChart.jpg
Two sketches are presented in this tutorial:
In the first sketch, the user wanted to be able to push a switch on pin 2 and have LED6 toggle on/off. However, switch control of LED6 is a hit and miss proposition as other LED control takes up most of the controller’s processing time.
This sketch uses the delay() function which stops normal code exe…
Newcomers to Arduino, soon discover the usefulness of using the Blink Without Delay (BWD) technique for timing events.
As most old timers realize, adding a Flag to this technique can prevent subtle and often unanticipated problems.
For those newcomers who do not know what and why a Flag should be used, hopefully this discussion will shed some light on the topic.
In the code segment below, we are looking for a time interval to expire.
When we want to start event timing with BWD, we make ledOn…
See State Machine Programming .
This sketch introduces the concepts of:
macros
structure
enumeration
BWD (Blink Without Delay timing) technique
timers
State Machine
The purpose of this example is to integrate the above concepts into a sketch to see how they can be used to simplify coding and make that coding more efficient.
An important feature is you can control your Timers.
Under program control, Timers can be running, restarted and disabled.
When it comes to the ‘State Machine’, you can see how the sketch’s current st…
One of the servos only has to move from 0 to 45 degrees everytime the other servo elevates. The other servo has to be in 3 positions which are different angles. First this servo is at 0 degrees, then at 5, the other servo moves from 0 to 45 degrees, lets the ball fall thorugh the bar and the ir sensors calcukate speed and acceleration. Then, the second servo goes back to 0 degrees, and the first servo goes back to 0 degrees. Then repeats the whole process with 10 degrees of inclination and then 15 degrees. I honestly think that with one time doing all this sequence would be ok.
I know but I have been today 8 hours in front of the computer trying to solve it😭
Thanks I will read those!
Thanks for all that help, I will check those!
Koepel
August 29, 2021, 12:09am
14
This is a Wokwi simulation, because I wanted to see what the servos do.
It is the same sketch as in your first post, without any changes.
Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!
To be able to move servo motor slow, its position is often updated every 10ms or 20ms. Here is a example, start the simulation and press the buttons and turn the potentiometers: https://wokwi.com/arduino/projects/305087394119418434
system
Closed
December 27, 2021, 12:09am
15
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.