jt123yeet:
I read somewhere that if you differ the delay time, that makes the servo appear to go faster or slower. That is what I was trying to do with all the if statements at the end. When I uploaded the code to my Arduino Uno, nothing happened at all, the 4-digit display didn't even light up.
Hi
There's quite a lot going on here so let's see if we can break the big problem down inti more solvable chunks
- Check we are talking to the display. Add the following function:-
void displayTest() {
for(int i = 0; i < 4; i++) {
tm1637.display(i,i);
delay(1000);
}
delay(3000);
tm1637.clearDisplay();
}
Then add this line after your delay(1500); statement in setup()
displayTest();
This will prove that you can talk to the display and that things are displaying in the correct place. If nothing happens check your wiring carefully.
- Is the distance sensor working. First of all are you seeing anything on the serial monitor? If so does it make sense? i.e. if you point it at an object 30cm away does it show a value somewhere around 30? if not check your calculations. Try changing your equation to:-
distance = duration*34/2000);
N.B. duration should be an unsigned long to match what pulseIn() returns.
If it continuously shows 0 even though your target object is within range check your wiring.
As you only seem to be interested in short distances you may want to read the reference page on pulseIn() to set the timeout value so it doesn't spend too much time waiting for return echoes. N.B. duration should be an unsigned long to match what pulseIn() returns.
- There are two types of servo commonly used with the arduino i) fixed rotation which moves from 0 to 180 degrees on command and ii) Continuous rotation which rotates either forward or backwards at a speed controlled by the pulse width modulation. As your talking about servo speed I'm wondering if you are using a continuous rotation version?
To drive a servo you need to provide it with a pulse width modulated square wave. The easiest way to do this is to use the servo library provided with the arduino IDE.
To do this add the following lines to your code
// At the top of the code
#include <Servo.h>
Servo myServo;
// In setup();
myServo.attach(servo);
// In loop when you want to change the servo position or speed
myServo.write([i]position[/i]);
// [i]position[/i] varies from 0 to 180 where 0 is fully anticlockwise and 180 is fully clockwise for the fixed rotation version servo
// and 0 is max speed anticlockwise, 90 is stopped and 180 is max speed clockwise for the continuous rotation version servo.
N.B. Do not power more than one small (e.g. SG90) servo from the arduino as taking too much current from the arduino will damage it. This is especially true if using two continuous rotation servos to drive a small vehicle.
Finally you are comparing distance to fixed values in your code e.g.
if ((distance) == 5) {
delay(500);
}
As distance is continuously varying it is very likely most of the time it will be either greater than or less than a particular value rather than actually equal to it. You should test that it falls in a range of values e.g.
if ((distance < 5) & (distance > 0)) {
// Do something because it's very close
}
if (distance < 7) & (distance => 5)) {
// Do something because it's getting near
}
Note that 0 is a special case which is returned by pulseIn() when it times out and actually indicates a distance beyond maximum range (controlled by the timeout parameter in pulseIn() which defaults to 1sec if not set).
Hope this gives you some guidance to help you get your code working.
Ian