How to tell about function "Stop" being true?

Basically I'm doing a very very basic command (a remote car), and I wanted to do so every 10 seconds of inactivity on the inputs, it randomizes movement until a input is inserted. I am pretty sure on how to do the randomizer one but I just cant seem to tell how to write "the robot isn't receiving any inputs" to arduino.

Here's the code:

#include <TimeLib.h>
int r=random(1, 8);
int c=random(0, 5000);
int d=random(0,5000);
void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  Stop();
}

void loop() {
  time_t t=now();
  Serial.println(analogRead(A0));
  Serial.println(analogRead(A1));
  Serial.println(analogRead(A2));
  Serial.println(analogRead(A3));
   if (analogRead(A0)<1)
   {
    backward();
   }
   else if (analogRead(A1)<1)
   {
    forward();
   }
   else if (analogRead(A2)<1)
   {
    left();
   }
   else if (analogRead(A3)<1)
   {
    right();
   }
   else {
    Stop();
   }
   while (t>=10 && /*i am not sure of what to put here*/)
   {
    /*here is the random code, of which i didnt do yet*/
   }
}
void forward()
{
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);

}

void backward()
{
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);

}
void left()
{
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);

}
void right()
{
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);

}
void Stop()
{
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);

}
void start()
{
   if (analogRead(A0)<1)
   {
    backward();
   }
   else if (analogRead(A1)<1)
   {
    forward();
   }
   else if (analogRead(A2)<1)
   {
    left();
   }
   else if (analogRead(A3)<1)
   {
    right();
   }
}

Do you mean you want to know if the car is stopped or the stop() function is being called?

Every time an input is received update a variable with the millis() time.
Every iteration of the loop subtract that variable from the current time, if the difference is greater than 10,000 then perform random actions while polling for inputs.

How do you define "inactivity" - do you mean the analog read values dont change?
You will need to set a threshold as analog inputs generally are noisy.

If you're looking for the analog inputs to be <1, which means 0, makes me wonder what sensors you have on there? Maybe just switches?- in which case a digital read for high/low would probably be more appropriate.

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