i am trying to create a vilros sg90 servo controlled by a hc-sr04 ultrasonic sensor on my arduino uno.
i dont understand what I am doing wrong? I want at a certain distance, the servo begins to rotate, and follow your proximity via the angle of the servo. can someone overlook the code?
int val = digitalRead(distance); //tells ardiuno to read distance as val
val = map(val, 300, 500, 0, 179); //define the value of distance to angle of servo arm
myservo.write(val); //servo functions based off of val
delay(15);
int val = digitalRead(distance); //tells ardiuno to read distance as val
val = map(val, 300, 500, 0, 179); //define the value of distance to angle of servo arm
digitalRead returns zero or one. What values are you getting for distance? If it's 300 to 500, you're asking the Arduino what it has on pins in that range - not likely to work well.
Drop the digitalRead. I'd imagine the map was supposed to be:
val = map(distance, 300, 500, 0, 179); //define the value of distance to angle of servo arm