87Views0Replies

Author Options:

[Arduino] Need some help with a few lines of code. Answered

Hi!

I have put together a nasty little robot over the past few days with an arduino, ardumoto shield, HCSR04 Ping Sensor, and a  beat up RC car. Currently I have it scrambling around the floor bashing into things and want to add even more unreliable muck to its maneuvers. Here is my code so far:

#define trigPin 9
#define echoPin 10

int maxrange = 200;
int minrange = 50;
long duration, distance;

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop()
{
  digitalWrite(trigPin, LOW);
  delay(2);

  digitalWrite(trigPin, HIGH);
  delay(10);

  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);

  distance = duration/58.2;

  if (distance >= minrange){
  digitalWrite(12, LOW);
  analogWrite(3, 255);
}
else {
  digitalWrite(12, HIGH);
  analogWrite(3, 255);
}
}

All this does is make it go back and forth in regards to objects, but the lack of any tread on its wheels make it spin around quite a bit. I want to add in random movements of the front steering wheels, but I don't know much about coding and even less about making code choose randomly. Any help?

Comments

The forums are retiring in 2021 and are now closed for new topics and comments.