Error in my code help

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();

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?

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 ! :frowning:

Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

See BWD:

See State Machine Programming .

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!

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.

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

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