Randomly stop my arduino car

Hey! for school i need to create a robot were almost done but it needs to stop somewere in between 0 and 120 seconds and reset once turned off, could someone please tell me how I incorparate this in my code or provide the information neccecary on how to make it work? is the code as if right now
#define IR_SENSOR_RIGHT 11
#define IR_SENSOR_LEFT 12
#define MOTOR_SPEED 180

//Right motor
int enableRightMotor=6;
int rightMotorPin1=7;
int rightMotorPin2=8;

//Left motor
int enableLeftMotor=5;
int leftMotorPin1=9;
int leftMotorPin2=10;

void setup()
{
//The problem with TT gear motors is that, at very low pwm value it does not even rotate.
//If we increase the PWM value then it rotates faster and our robot is not controlled in that speed and goes out of line.
//For that we need to increase the frequency of analogWrite.
//Below line is important to change the frequency of PWM signal on pin D5 and D6
//Because of this, motor runs in controlled manner (lower speed) at high PWM value.
//This sets frequecny as 7812.5 hz.
TCCR0B = TCCR0B & B11111000 | B00000010 ;

// put your setup code here, to run once:
pinMode(enableRightMotor, OUTPUT);
pinMode(rightMotorPin1, OUTPUT);
pinMode(rightMotorPin2, OUTPUT);

pinMode(enableLeftMotor, OUTPUT);
pinMode(leftMotorPin1, OUTPUT);
pinMode(leftMotorPin2, OUTPUT);

pinMode(IR_SENSOR_RIGHT, INPUT);
pinMode(IR_SENSOR_LEFT, INPUT);
rotateMotor(0,0);
}

void loop()
{

int rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
int leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);

//If none of the sensors detects black line, then go straight
if (rightIRSensorValue == LOW && leftIRSensorValue == LOW)
{
rotateMotor(MOTOR_SPEED, MOTOR_SPEED);
}
//If right sensor detects black line, then turn right
else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW )
{
rotateMotor(-MOTOR_SPEED, MOTOR_SPEED);
}
//If left sensor detects black line, then turn left
else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH )
{
rotateMotor(MOTOR_SPEED, -MOTOR_SPEED);
}
//If both the sensors detect black line, then stop
else
{
rotateMotor(0, 0);
}
}

void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
{

if (rightMotorSpeed < 0)
{
digitalWrite(rightMotorPin1,LOW);
digitalWrite(rightMotorPin2,HIGH);
}
else if (rightMotorSpeed > 0)
{
digitalWrite(rightMotorPin1,HIGH);
digitalWrite(rightMotorPin2,LOW);
}
else
{
digitalWrite(rightMotorPin1,LOW);
digitalWrite(rightMotorPin2,LOW);
}

if (leftMotorSpeed < 0)
{
digitalWrite(leftMotorPin1,LOW);
digitalWrite(leftMotorPin2,HIGH);
}
else if (leftMotorSpeed > 0)
{
digitalWrite(leftMotorPin1,HIGH);
digitalWrite(leftMotorPin2,LOW);
}
else
{
digitalWrite(leftMotorPin1,LOW);
digitalWrite(leftMotorPin2,LOW);
}
analogWrite(enableRightMotor, abs(rightMotorSpeed));
analogWrite(enableLeftMotor, abs(leftMotorSpeed));
}
thanks in advance

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

You might want to look at this How to get the best out of this forum before you proceed any further.

It tells you how to post code on this forum.

It resets automatically when you turn it back on again.

Did what ?

Whatever it was you have ignored the advice to use code tags when posting code. Please edit your post and add them

Why are you ignoring the advice to post code correctly?

This is not a good idea:

stopTime = millis() + random(1000, 180000);

//...

if (!stopped && millis() >= stopTime) 

If you want to read how timer registers work, here are some links.

Show and tell:

How PWM works:
https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm/

// call a function for a random time less than two minutes then stop and wait for RESET

unsigned long start, stop, twominutes = 120; // keep track of time

void setup() {
  Serial.begin(115200); // start serial communication
  randomSeed(analogRead(A0)); // increase pseudo-randomness
  pinMode(LED_BUILTIN, OUTPUT); // configure LED_BUILTIN for output

  start = millis(); // store "start" time
  stop = (random(twominutes) + 1) * 1000UL; // store "stop" time greater than zero
  Serial.print("START "); Serial.print(start); // print start time
  Serial.print(" STOP "); Serial.print(stop / 1000UL); // print stop time
}

void loop() {
  if (millis() - start < stop) { // if "now" minus "start" is less than "stop" time
    yourfunction(); // call your function
  } else {
    stopfunction(); // call stop function
  }
}

void yourfunction() {
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // blink LED_BUILTIN
  delay(250);
}

void stopfunction() {
  Serial.print(" DONE "); Serial.print(millis() / 1000UL);
  Serial.println(" Press RESET");
  while (1); // stay here, wait for reset
}

This page has a neat anamation showing the change in avrage voltage with PWM duty cycle.

PWM tutorial

The photo above the animation is even neater...

1 Like

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