Offline
Newbie
Karma: 0
Posts: 6
|
 |
« on: December 24, 2012, 03:53:52 am » |
int enableA1 = 2; int enableA2 = 3; int pinA1 = 1; int pinA2 = 0; int enableB1 = 7; int enableB2 = 8; int pinB1 = 6; int pinB2 = 5; int sensorpin=A0; int value=0; void setup() { Serial.begin(9600); pinMode(enableA1, OUTPUT); pinMode(enableA2, OUTPUT); pinMode(pinA1, OUTPUT); pinMode(pinA2, OUTPUT); pinMode(enableB1, OUTPUT); pinMode(enableB2, OUTPUT); pinMode(pinB1, OUTPUT); pinMode(pinB2, OUTPUT); } void loop() { int analogValue = analogRead(sensorpin); motorAOn(); //motorAOn(); //motorAForward(); if(analogValue > 550) { motorABackward(); //motorAOff(); //motorAForward(); } else { motorAForward(); } }
void motorAOn() { digitalWrite(enableA1, HIGH); digitalWrite(enableA2, HIGH); } void motorBOn() { digitalWrite(enableB1, HIGH); digitalWrite(enableB2, HIGH); } void motorAOff() { digitalWrite(enableA1, LOW); digitalWrite(enableA2, LOW); delay(1000); } void motorBOff() { digitalWrite(enableB1, LOW); digitalWrite(enableB2, LOW); } void motorAForward() { digitalWrite(pinA1, HIGH); digitalWrite(pinA2, LOW); } void motorABackward() { digitalWrite(pinA1, LOW); digitalWrite(pinA2, HIGH); delay(3000); } void motorABrake() { digitalWrite(pinA1, HIGH); digitalWrite(pinA2, HIGH); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #1 on: December 24, 2012, 03:55:17 am » |
this is not syntax error,it is logic error the compiler doesnot go in if -statement
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9414
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: December 24, 2012, 04:02:57 am » |
What is connected to the analog pin? How did you connect it? please post schematic (you can attach an image to the post)
if you connect +5V to the analog pin what does the sketch do? if you connect GND to the analog pin what does the sketch do?
Please use the # button to get code taggs around your code, makes it more readable. (you can modify earlier post to do it still) Thank you
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #3 on: December 24, 2012, 04:48:35 am » |
this an image for my project
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: December 24, 2012, 04:48:56 am » |
the compiler doesnot go in if -statement That's a new one. Please read the sticky post at the top of this section.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: December 24, 2012, 04:51:12 am » |
It's a pity you used the serial pins for other purposes - it's much easier to debug a sketch with prints than it is by observation.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #6 on: December 24, 2012, 05:39:36 am » |
thank u ver much 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #7 on: December 24, 2012, 11:34:23 am » |
why the motor does not work?! thanx in advance 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35521
Seattle, WA USA
|
 |
« Reply #8 on: December 24, 2012, 11:46:06 am » |
why the motor does not work?! It was suggested that you change the code to use other pins? Did you? It was suggested that you add debug statements. Did you? If not, come back when you have. If you have, you need to show us the revised code and the debug output.
|
|
|
|
|
Logged
|
|
|
|
|
Saskatchewan
Offline
Full Member
Karma: 10
Posts: 223
When the going gets weird, the weird turn pro. - Hunter S. Thompson
|
 |
« Reply #9 on: December 24, 2012, 12:12:43 pm » |
If I were you I'd actually knock out a little sketch to test the readings with nothing else. void setup(){ Serial.begin(9600); }
void loop(){ Serial.println(analogRead(0)); delay(2000); // read every two seconds }
If you look at that and check out the reference for the analogRead() function you may see your problem.
|
|
|
|
« Last Edit: December 24, 2012, 12:15:27 pm by Jimmy60 »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #10 on: December 24, 2012, 11:51:27 pm » |
in this code motor A is turn on and make backward when sensor read a near distance but motor B not make any action please any idea to help me?! thanx in advance int enableA1 = 8; int enableA2 = 9; int pinA1 = 10; int pinA2 = 11; int enableB1 = 12; int enableB2 = 13; int pinB1 = 2; int pinB2 = 7; int sensorpin=A0; int value=0; void setup() { Serial.begin(9600); pinMode(enableA1, OUTPUT); pinMode(enableA2, OUTPUT); pinMode(pinA1, OUTPUT); pinMode(pinA2, OUTPUT); pinMode(enableB1, OUTPUT); pinMode(enableB2, OUTPUT); pinMode(pinB1, OUTPUT); pinMode(pinB2, OUTPUT); } void loop() { int analogValue = analogRead(sensorpin); motorAOn(); motorBOn(); //motorAForward(); if(analogValue > 100) { //motorABackward(); Serial.println(analogValue); //motorAOff(); motorABackward(); motorBForward(); //delay(3000); } else if(analogValue < 100) { Serial.println(analogValue); motorAForward(); motorBForward(); //Serial.println(analogValue); //delay(3000); } delay(1000); }
// Yu Hin Hau // Robotic Car via H-Bridge (L298) // June 5, 2012
//See Low Level for Command Definitions
//Define Pins
//Define Low Level H-Bridge Commands
//enable motors void motorAOn() { digitalWrite(enableA1, HIGH); digitalWrite(enableA2, HIGH); }
void motorBOn() { digitalWrite(enableB1, HIGH); digitalWrite(enableB2, HIGH); }
//disable motors void motorAOff() { digitalWrite(enableA1, LOW); digitalWrite(enableA2, LOW); }
void motorBOff() { digitalWrite(enableB1, LOW); digitalWrite(enableB2, LOW); }
//motor A controls void motorAForward() { digitalWrite(pinA1, HIGH); digitalWrite(pinA2, LOW); Serial.println("In forword"); //delay(1000); }
void motorABackward() { digitalWrite(pinA1, LOW); digitalWrite(pinA2, HIGH); Serial.println("In backword"); //delay(1000); }
//motor B contorls void motorBForward() { digitalWrite(pinB1, HIGH); digitalWrite(pinB2, LOW); }
void motorBBackward() { digitalWrite(pinB1, LOW); digitalWrite(pinB2, HIGH); }
//coasting and braking void motorACoast() { digitalWrite(pinA1, LOW); digitalWrite(pinA2, LOW); }
void motorABrake() { digitalWrite(pinA1, HIGH); digitalWrite(pinA2, HIGH); }
void motorBCoast() { digitalWrite(pinB1, LOW); digitalWrite(pinB2, LOW); }
void motorBBrake() { digitalWrite(pinB1, HIGH); digitalWrite(pinB2, HIGH); }
//Define High Level Commands
void enableMotors() { motorAOn(); motorBOn(); }
void disableMotors() { motorAOff(); motorBOff(); }
void forward(int time) { motorAForward(); motorBForward(); delay(time); }
void backward(int time) { motorABackward(); motorBBackward(); delay(time); }
void turnLeft(int time) { motorABackward(); motorBForward(); delay(time); }
void turnRight(int time) { motorAForward(); motorBBackward(); delay(time); }
void coast(int time) { motorACoast(); motorBCoast(); delay(time); }
void brake(int time) { motorABrake(); motorBBrake(); delay(time); }
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 48
Posts: 1420
May all of your blinks be without delay
|
 |
« Reply #11 on: December 25, 2012, 03:05:11 am » |
I can't see anything in your program that would make motor B do anything but run forward no matter what value is returned by the sensor assuming, that is, that motorBForward(); does what its name implies. void loop() { int analogValue = analogRead(sensorpin); motorAOn(); motorBOn(); //motorAForward(); if(analogValue > 100) { //motorABackward(); Serial.println(analogValue); //motorAOff(); motorABackward(); motorBForward(); //delay(3000); } else if(analogValue < 100) { Serial.println(analogValue); motorAForward(); motorBForward(); //Serial.println(analogValue); //delay(3000); } delay(1000); }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35521
Seattle, WA USA
|
 |
« Reply #12 on: December 25, 2012, 09:20:03 am » |
You need simpler code: int enableB1 = 12; int enableB2 = 13; int pinB1 = 2; int pinB2 = 7;
void setup() { Serial.begin(9600); pinMode(enableB1, OUTPUT); pinMode(enableB2, OUTPUT); pinMode(pinB1, OUTPUT); pinMode(pinB2, OUTPUT);
motorBOn(); motorBForward(); }
void loop() { } If motor B does not move, it is not a programming issue.
|
|
|
|
|
Logged
|
|
|
|
|
|