Go Down

Topic: urgent, please help (Read 371 times) previous topic - next topic

long213

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..



Code: [Select]
#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();   
 
 }
}

J-M-L

Hello

space and tab keys broken on your keyboard? too difficult to read that way... needs to be better indented
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

long213

Code: [Select]
#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();   
 
 }
}

AWOL

Quote
needs to be better indented
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

long213

Code: [Select]
#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();   
 
 }
}

UKHeliBob

What prevents the moveForward(); function being called each time through loop() ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

long213

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?

J-M-L

Code: [Select]
if (engineCanRun) moveForward();
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

long213

Code: [Select]
if (engineCanRun) moveForward();
Sorry I didn't understand.
Would you please give me more details?

Gabriel_swe

This line can be simplified

Code: [Select]
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
Code: [Select]
}

{

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

UKHeliBob

Quote
There is not any code that prevents the moveForward(); function being called each time through loop().
No wonder the motor continues to run.

Quote
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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

long213

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. :smiley-confuse:

sterretje

What would you do if it wad a butyo?

Code: [Select]

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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

UKHeliBob

I still do not know how to make the call to moveForward() dependant on it being true. :smiley-confuse:
Create a global boolean variable at the start of the program
Code: [Select]
boolean engineCanRun = true;
Test it to see if it is OK to run the engine
Code: [Select]
if (engineCanRun == true)  //only move if it is OK to do so.
{
  moveForward();
}

To stop the engines
Code: [Select]
engineCanRun = false;
anywhere in the code.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up