urgent, please help

Hi, everyone. I have been working with a ultrasonic obstacle avoidance car project with load cell weight measurement fuction. And I have problem with how to stop the motors after 15 seconds. I tried the following sketch but it does not work. The motor still keeps running. Can anyone help me with the code? please..

#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(48, 49, 50, 51, 52, 53);
#define DT A14
#define SCK A15
#define sw 22
#define LED1 45
#define LED2 46
#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 300
#define MAX_SPEED 160
#define MAX_SPEED_OFFSET 40
#define COLL_DIST 30
#define TURN_DIST COLL_DIST+20
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor leftMotor1(1, MOTOR12_1KHZ);
AF_DCMotor leftMotor2(2, MOTOR12_1KHZ);
AF_DCMotor rightMotor1(3, MOTOR34_1KHZ);
AF_DCMotor rightMotor2(4, MOTOR34_1KHZ);
Servo myservo;
int leftDistance, rightDistance;
int curDist = 0;
String motorSet = "";
int speedSet = 0;
long sample=0;
float val=0;
long count=0;
long previousMillis = 0;
long interval = 15000;
unsigned long readCount(void)
{
unsigned long Count;
unsigned char i;
pinMode(DT, OUTPUT);
digitalWrite(DT,HIGH);
digitalWrite(SCK,LOW);
Count=0;
pinMode(DT, INPUT);
while(digitalRead(DT));
for (i=0;i<24;i++)
{
digitalWrite(SCK,HIGH);
Count=Count<<1;
digitalWrite(SCK,LOW);
if(digitalRead(DT))
Count++;
}
digitalWrite(SCK,HIGH);
Count=Count^0x800000;
digitalWrite(SCK,LOW);
return(Count);
}
void calibrate()
{
lcd.clear();
lcd.print("Calibrating...");
lcd.setCursor(0,1);
lcd.print("Please Wait...");
for(int i=0;i<100;i++)
{
count=readCount();
sample+=count;
Serial.println(count);
}
sample/=100;
Serial.print("Avg:");
Serial.println(sample);
lcd.clear();
lcd.print("Put 100g & wait");
count=0;
while(count<1000)
{
count=readCount();
count=sample-count;
Serial.println(count);
}
lcd.clear();
lcd.print("Please Wait....");
delay(2000);
for(int i=0;i<100;i++)
{
count=readCount();
val+=sample-count;
Serial.println(sample-count);
}
val=val/100.0;
val=val/100.0;
lcd.clear();
}
int readPing() {
delay(70);
unsigned int uS = sonar.ping();
int cm = uS/US_ROUNDTRIP_CM;
return cm;
}
void moveStop() {
leftMotor1.run(RELEASE);
leftMotor2.run(RELEASE);
rightMotor1.run(RELEASE);
rightMotor2.run(RELEASE);
}
void moveForward() {
motorSet = "FORWARD";
leftMotor1.run(FORWARD);
leftMotor2.run(FORWARD);
rightMotor1.run(FORWARD);
rightMotor2.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
{
leftMotor1.setSpeed(speedSet);
leftMotor2.setSpeed(speedSet);
rightMotor1.setSpeed(speedSet);
rightMotor2.setSpeed(speedSet);
delay(5);
}
}
void turnRight() {
motorSet = "RIGHT";
leftMotor1.run(FORWARD);
leftMotor2.run(FORWARD);
rightMotor1.run(BACKWARD);
rightMotor2.run(BACKWARD);
rightMotor1.setSpeed(speedSet+MAX_SPEED_OFFSET);
rightMotor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
delay(1500);
motorSet = "FORWARD";
leftMotor1.run(FORWARD);
leftMotor2.run(FORWARD);
rightMotor1.run(FORWARD);
rightMotor2.run(FORWARD);
}
void turnLeft() {
motorSet = "LEFT";
leftMotor1.run(BACKWARD);
leftMotor2.run(BACKWARD);
leftMotor1.setSpeed(speedSet+MAX_SPEED_OFFSET);
leftMotor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
rightMotor1.run(FORWARD);
rightMotor2.run(FORWARD);
delay(1500);
motorSet = "FORWARD";
leftMotor1.run(FORWARD);
leftMotor2.run(FORWARD);
rightMotor1.run(FORWARD);
rightMotor2.run(FORWARD);
}
void changePath() {
moveStop();
myservo.write(36);
delay(500);
rightDistance = readPing();
delay(500);
myservo.write(144);
delay(700);
leftDistance = readPing();
delay(500);
myservo.write(90);
delay(100);
compareDistance();
}
void compareDistance()
{
if (leftDistance>rightDistance)
{
turnLeft();
}
else if (rightDistance>leftDistance)
{
turnRight();
}
}
void setup()
{
Serial.begin(9600);
pinMode(SCK, OUTPUT);
pinMode(sw, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
lcd.begin(16, 2);
lcd.print("SZPT Project Gp4");
lcd.setCursor(0,1);
lcd.print(" No. 1");
delay(4000);
lcd.clear();
calibrate();
myservo.attach(10);
myservo.write(90);
delay(1000);
}
void loop()
{
count= readCount();
int w=(((count-sample)/val)-2*((count-sample)/val));
w = max(w,0);
Serial.print("weight:");
Serial.print((int)w);
Serial.println("g");
lcd.setCursor(0,0);
lcd.print("Weight ");
lcd.setCursor(0,1);
lcd.print(w);
lcd.print("g ");
if(digitalRead(sw)==0)
{
val=0;
sample=0;
w=0;
count=0;
calibrate();
}
if((((count-sample)/val)-2*((count-sample)/val))>=80){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
}
else{
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
}
{
myservo.write(90);
delay(90);
curDist = readPing();
if (curDist < COLL_DIST) {changePath();}
moveForward();
delay(500);
}

if (millis() - previousMillis > interval) {
   moveStop();
   previousMillis = millis();   
  
 }
}

Hello

space and tab keys broken on your keyboard? too difficult to read that way... needs to be better indented

#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>
#include <LiquidCrystal.h>


LiquidCrystal lcd(48, 49, 50, 51, 52, 53);


#define DT A14
#define SCK A15
#define sw 22
#define LED1 45
#define LED2 46
#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 300
#define MAX_SPEED 160
#define MAX_SPEED_OFFSET 40
#define COLL_DIST 30
#define TURN_DIST COLL_DIST+20


NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);


AF_DCMotor leftMotor1(1, MOTOR12_1KHZ);
AF_DCMotor leftMotor2(2, MOTOR12_1KHZ);
AF_DCMotor rightMotor1(3, MOTOR34_1KHZ);
AF_DCMotor rightMotor2(4, MOTOR34_1KHZ);


Servo myservo;


int leftDistance, rightDistance;
int curDist = 0;
String motorSet = "";
int speedSet = 0;
long sample=0;
float val=0;
long count=0;
long previousMillis = 0;
long interval = 15000;


unsigned long readCount(void)
{
unsigned long Count;
unsigned char i;
pinMode(DT, OUTPUT);
digitalWrite(DT,HIGH);
digitalWrite(SCK,LOW);
Count=0;
pinMode(DT, INPUT);
while(digitalRead(DT));
for (i=0;i<24;i++)
{
digitalWrite(SCK,HIGH);
Count=Count<<1;
digitalWrite(SCK,LOW);
if(digitalRead(DT))
Count++;
}
digitalWrite(SCK,HIGH);
Count=Count^0x800000;
digitalWrite(SCK,LOW);
return(Count);
}


void calibrate()
{
lcd.clear();
lcd.print("Calibrating...");
lcd.setCursor(0,1);
lcd.print("Please Wait...");
for(int i=0;i<100;i++)
{
count=readCount();
sample+=count;
Serial.println(count);
}
sample/=100;
Serial.print("Avg:");
Serial.println(sample);
lcd.clear();
lcd.print("Put 100g & wait");
count=0;
while(count<1000)
{
count=readCount();
count=sample-count;
Serial.println(count);
}
lcd.clear();
lcd.print("Please Wait....");
delay(2000);
for(int i=0;i<100;i++)
{
count=readCount();
val+=sample-count;
Serial.println(sample-count);
}
val=val/100.0;
val=val/100.0;
lcd.clear();
}


int readPing() {
delay(70);
unsigned int uS = sonar.ping();
int cm = uS/US_ROUNDTRIP_CM;
return cm;
}


void moveStop() {
leftMotor1.run(RELEASE);
leftMotor2.run(RELEASE);
rightMotor1.run(RELEASE);
rightMotor2.run(RELEASE);
}


void moveForward() {
motorSet = "FORWARD";
leftMotor1.run(FORWARD);
leftMotor2.run(FORWARD);
rightMotor1.run(FORWARD);
rightMotor2.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
{
leftMotor1.setSpeed(speedSet);
leftMotor2.setSpeed(speedSet);
rightMotor1.setSpeed(speedSet);
rightMotor2.setSpeed(speedSet);
delay(5);
}
}


void turnRight() {
motorSet = "RIGHT";
leftMotor1.run(FORWARD);
leftMotor2.run(FORWARD);
rightMotor1.run(BACKWARD);
rightMotor2.run(BACKWARD);
rightMotor1.setSpeed(speedSet+MAX_SPEED_OFFSET);
rightMotor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
delay(1500);
motorSet = "FORWARD";
leftMotor1.run(FORWARD);
leftMotor2.run(FORWARD);
rightMotor1.run(FORWARD);
rightMotor2.run(FORWARD);
}


void turnLeft() {
motorSet = "LEFT";
leftMotor1.run(BACKWARD);
leftMotor2.run(BACKWARD);
leftMotor1.setSpeed(speedSet+MAX_SPEED_OFFSET);
leftMotor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
rightMotor1.run(FORWARD);
rightMotor2.run(FORWARD);
delay(1500);
motorSet = "FORWARD";
leftMotor1.run(FORWARD);
leftMotor2.run(FORWARD);
rightMotor1.run(FORWARD);
rightMotor2.run(FORWARD);
}


void changePath() {
moveStop();
myservo.write(36);
delay(500);
rightDistance = readPing();
delay(500);
myservo.write(144);
delay(700);
leftDistance = readPing();
delay(500);
myservo.write(90);
delay(100);
compareDistance();
}


void compareDistance()
{
if (leftDistance>rightDistance)
{
turnLeft();
}
else if (rightDistance>leftDistance)
{
turnRight();
}
}


void setup()
{
Serial.begin(9600);
pinMode(SCK, OUTPUT);
pinMode(sw, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
lcd.begin(16, 2);
lcd.print("SZPT Project Gp4");
lcd.setCursor(0,1);
lcd.print(" No. 1");
delay(4000);
lcd.clear();
calibrate();
myservo.attach(10);
myservo.write(90);
delay(1000);
}


void loop()
{
count= readCount();
int w=(((count-sample)/val)-2*((count-sample)/val));
w = max(w,0);
Serial.print("weight:");
Serial.print((int)w);
Serial.println("g");
lcd.setCursor(0,0);
lcd.print("Weight ");
lcd.setCursor(0,1);
lcd.print(w);
lcd.print("g ");
if(digitalRead(sw)==0)
{
val=0;
sample=0;
w=0;
count=0;
calibrate();
}
if((((count-sample)/val)-2*((count-sample)/val))>=80){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
}
else{
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
}
{
myservo.write(90);
delay(90);
curDist = readPing();
if (curDist < COLL_DIST) {changePath();}
moveForward();
delay(500);
}

if (millis() - previousMillis > interval) {
   moveStop();
   previousMillis = millis();   
  
 }
}

needs to be better indented

#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>
#include <LiquidCrystal.h>


LiquidCrystal lcd(48, 49, 50, 51, 52, 53);


#define DT A14
#define SCK A15
#define sw 22
#define LED1 45
#define LED2 46
#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 300
#define MAX_SPEED 160
#define MAX_SPEED_OFFSET 40
#define COLL_DIST 30
#define TURN_DIST COLL_DIST+20


NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);


AF_DCMotor leftMotor1(1, MOTOR12_1KHZ);
AF_DCMotor leftMotor2(2, MOTOR12_1KHZ);
AF_DCMotor rightMotor1(3, MOTOR34_1KHZ);
AF_DCMotor rightMotor2(4, MOTOR34_1KHZ);


Servo myservo;


int leftDistance, rightDistance;
int curDist = 0;
String motorSet = "";
int speedSet = 0;
long sample=0;
float val=0;
long count=0;
long previousMillis = 0;
long interval = 15000;


unsigned long readCount(void)
{
 unsigned long Count;
 unsigned char i;
 pinMode(DT, OUTPUT);
 digitalWrite(DT,HIGH);
 digitalWrite(SCK,LOW);
 Count=0;
 pinMode(DT, INPUT);
 while(digitalRead(DT));
 for (i=0;i<24;i++)
  {
    digitalWrite(SCK,HIGH);
    Count=Count<<1;
    digitalWrite(SCK,LOW);
    if(digitalRead(DT))
    Count++;
   }
 digitalWrite(SCK,HIGH);
 Count=Count^0x800000;
 digitalWrite(SCK,LOW);
 return(Count);
}


void calibrate()
{
 lcd.clear();
 lcd.print("Calibrating...");
 lcd.setCursor(0,1);
 lcd.print("Please Wait...");
 for(int i=0;i<100;i++)
   {
    count=readCount();
    sample+=count;
    Serial.println(count);
   }
 sample/=100;
 Serial.print("Avg:");
 Serial.println(sample);
 lcd.clear();
 lcd.print("Put 100g & wait");
 count=0;
 while(count<1000)
   {
     count=readCount();
     count=sample-count;
     Serial.println(count);
   }
 lcd.clear();
 lcd.print("Please Wait....");
 delay(2000);
 for(int i=0;i<100;i++)
   {
     count=readCount();
     val+=sample-count;
     Serial.println(sample-count);
   }
 val=val/100.0;
 val=val/100.0;
 lcd.clear();
}


int readPing() {
 delay(70);
 unsigned int uS = sonar.ping();
 int cm = uS/US_ROUNDTRIP_CM;
 return cm;
}


void moveStop() {
 leftMotor1.run(RELEASE);
 leftMotor2.run(RELEASE);
 rightMotor1.run(RELEASE);
 rightMotor2.run(RELEASE);
}


void moveForward() {
 motorSet = "FORWARD";
 leftMotor1.run(FORWARD);
 leftMotor2.run(FORWARD);
 rightMotor1.run(FORWARD);
 rightMotor2.run(FORWARD);
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
   {
    leftMotor1.setSpeed(speedSet);
    leftMotor2.setSpeed(speedSet);
    rightMotor1.setSpeed(speedSet);
    rightMotor2.setSpeed(speedSet);
    delay(5);
   }
}


void turnRight() {
 motorSet = "RIGHT";
 leftMotor1.run(FORWARD);
 leftMotor2.run(FORWARD);
 rightMotor1.run(BACKWARD);
 rightMotor2.run(BACKWARD);
 rightMotor1.setSpeed(speedSet+MAX_SPEED_OFFSET);
 rightMotor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
 delay(1500);
 motorSet = "FORWARD";
 leftMotor1.run(FORWARD);
 leftMotor2.run(FORWARD);
 rightMotor1.run(FORWARD);
 rightMotor2.run(FORWARD);
}


void turnLeft() {
 motorSet = "LEFT";
 leftMotor1.run(BACKWARD);
 leftMotor2.run(BACKWARD);
 leftMotor1.setSpeed(speedSet+MAX_SPEED_OFFSET);
 leftMotor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
 rightMotor1.run(FORWARD);
 rightMotor2.run(FORWARD);
 delay(1500); 
 motorSet = "FORWARD";
 leftMotor1.run(FORWARD);
 leftMotor2.run(FORWARD);
 rightMotor1.run(FORWARD);
 rightMotor2.run(FORWARD);
}


void changePath() {
 moveStop(); 
 myservo.write(36);
 delay(500);
 rightDistance = readPing();
 delay(500); 
 myservo.write(144);
 delay(700);
 leftDistance = readPing();
 delay(500);
 myservo.write(90);
 delay(100);
 compareDistance();
}


void compareDistance()
{
  if (leftDistance>rightDistance)
   {
    turnLeft();
   }
  else if (rightDistance>leftDistance)
   {
    turnRight();
   }
}


void setup()
{
 Serial.begin(9600);
 pinMode(SCK, OUTPUT);
 pinMode(sw, INPUT_PULLUP);
 pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, HIGH);
 lcd.begin(16, 2);
 lcd.print("SZPT Project Gp4");
 lcd.setCursor(0,1);
 lcd.print(" No. 1");
 delay(4000);
 lcd.clear();
 calibrate();
 myservo.attach(10); 
 myservo.write(90);
 delay(1000);
}


void loop()
{
 count= readCount();
 int w=(((count-sample)/val)-2*((count-sample)/val)); 
 w = max(w,0);
 Serial.print("weight:");
 Serial.print((int)w);
 Serial.println("g");
 lcd.setCursor(0,0);
 lcd.print("Weight ");
 lcd.setCursor(0,1);
 lcd.print(w);
 lcd.print("g ");
 if(digitalRead(sw)==0)
  {
   val=0;
   sample=0; 
   w=0;
   count=0;
   calibrate();
  }
 if((((count-sample)/val)-2*((count-sample)/val))>=80){
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, LOW);
}

else{
 digitalWrite(LED1, LOW);
 digitalWrite(LED2, HIGH);
 }

{
 myservo.write(90);
 delay(90);
 curDist = readPing();
 if (curDist < COLL_DIST) {changePath();} 
 moveForward();
 delay(500);
}

if (millis() - previousMillis > interval) {
   moveStop();
   previousMillis = millis();   
  
 }
}

What prevents the moveForward(); function being called each time through loop() ?

UKHeliBob:
What prevents the moveForward(); function being called each time through loop() ?

There is not any code that prevents the moveForward(); function being called each time through loop().
May I ask how to modify the code?

if (engineCanRun) moveForward();

J-M-L:

if (engineCanRun) moveForward();

Sorry I didn't understand.
Would you please give me more details?

This line can be simplified

if((((count-sample)/val)-2*((count-sample)/val))>=80){

Compare it to x-2x >=80, -x >=80. In this case I assume x is negative so it becomes (-x)-2*(-x) >=80, -x >=80.

If I see these lines in my code

}

{

I start to look for mistakes. Ctrl-T is a very good shortcut. Try it in your code.

There is not any code that prevents the moveForward(); function being called each time through loop().

No wonder the motor continues to run.

Sorry I didn't understand.
Would you please give me more details?

If you don't want moveForward() to be called each time through loop() then create a boolean variable, perhaps named engineCanRun, and make the call to moveForward() dependant on it being true. When you want to prevent the vehicle moving set the boolean to false and the function will not be called

UKHeliBob:
No wonder the motor continues to run.
If you don't want moveForward() to be called each time through loop() then create a boolean variable, perhaps named engineCanRun, and make the call to moveForward() dependant on it being true. When you want to prevent the vehicle moving set the boolean to false and the function will not be called

I still do not know how to make the call to moveForward() dependant on it being true. :confused:

What would you do if it wad a butyo?

if (digitalRead(yourButton) == HIGH)
{
  moveForward()
}

Now replace the condition by something that reflects duration. Have a look at blink without delay, it uses (if I remember correctly) millis - previousMillis. In your case, as long as the duration is shorter than 15 seconds, the if must evaluate to true.

long213:
I still do not know how to make the call to moveForward() dependant on it being true. :confused:

Create a global boolean variable at the start of the program

boolean engineCanRun = true;

Test it to see if it is OK to run the engine

if (engineCanRun == true)  //only move if it is OK to do so.
{
  moveForward();
}

To stop the engines

engineCanRun = false;

anywhere in the code.