Long Island, NY
Offline
Newbie
Karma: 0
Posts: 39
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
|
 |
« on: March 31, 2012, 12:07:51 am » |
Arduino Uno, L298N Motor Shield, 2 Photo Resistors, Push Button, Two Small DC Motor (Out of a broken helicopter not sure of the voltage or draw, but pretty small). const int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3 (or 5,6) const int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 9 (or 10,11) const int dir_a = 12; //direction control for motor outputs 1 and 2 is on digital pin 2 (or 4,7) const int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 8 (or 12,13) const int button = 7; //Motor ON OFF button const int righteye = 0; // Right Photoresistor on PIN 0 const int lefteye = 1; // Left Photo Resistor on PIN 1
int lVal = 0; // used to store value from right eye int rVal = 0; // used to store value from right eye int val = 0; // val used to store button state int old_val = 0; //stores the old value int state = 0; // 0= LED OFF while 1 = LED ON
void setup() { pinMode(pwm_a, OUTPUT); //Set control pins to be outputs pinMode(pwm_b, OUTPUT); pinMode(dir_a, OUTPUT); pinMode(dir_b, OUTPUT); pinMode(button, INPUT); // button is an input }
void loop() { val = digitalRead(button); // read input value and store it
//check if there was a transition if ((val == HIGH) && (old_val == LOW)){ state = 1 - state; } old_val = val; // val is now old, store it // check if button is pressed (HIGH) // and chage the state if (state == 1) { rVal = analogRead(righteye); // Read value from right eye sensor lVal = analogRead(lefteye); // Read value from left eye sensor analogWrite(pwm_a, rVal/4); //set both motors to run at one quarter of light level analogWrite(pwm_b, lVal/4); } else { analogWrite(pwm_a, 0); //set both motors to OFF analogWrite(pwm_b, 0); } } no body yet
|
|
|
|
« Last Edit: March 31, 2012, 02:28:04 am by bytedisorder »
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 70
Posts: 6802
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #1 on: March 31, 2012, 07:20:02 am » |
Ummm, and you're posting because?
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Long Island, NY
Offline
Newbie
Karma: 0
Posts: 39
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
|
 |
« Reply #2 on: March 31, 2012, 08:42:29 pm » |
To get some feedback on the code, start a thread for the project. I'm still waiting for the wheels tracks and gearbox. First project with Arduino that's all.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 70
Posts: 6802
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #3 on: April 02, 2012, 12:41:12 am » |
At a glance your code looks OK, I gather it's supposed to follow a light?
There's no debouncing on the button but that may not worry you.
The motor speed is directly tied to the light level (rVal/4), that may or may not be appropriate when you get real hardware.
You are not using the dir pins.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 70
Posts: 6802
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #5 on: April 02, 2012, 02:58:04 am » |
Sorry I don't know enough about robots to advise on chassis etc, there may be others here who do or alternatively go over to the SoR forum. http://www.societyofrobots.com/robotforum/______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Long Island, NY
Offline
Newbie
Karma: 0
Posts: 39
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
|
 |
« Reply #6 on: April 02, 2012, 03:25:35 am » |
Thanks Rob, I appreciate the input. The only qualm I was having with button debouncing was that I cant have a delay using this method of motor control. const int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3 (or 5,6) const int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 9 (or 10,11) const int dir_a = 12; //direction control for motor outputs 1 and 2 is on digital pin 2 (or 4,7) const int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 8 (or 12,13) const int button = 7; //Motor ON OFF button const int righteye = 0; // Right Photoresistor on PIN 0 const int lefteye = 1; // Left Photo Resistor on PIN 1
int lVal = 0; // used to store value from right eye int rVal = 0; // used to store value from right eye int val = 0; // val used to store button state int old_val = 0; //stores the old value int state = 0; // 0= LED OFF while 1 = LED ON
long previousMillis = 0; // will store last time LED was updated // the follow variables is a long because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long interval = 1000; // interval at which to blink (milliseconds)
void setup() { Serial.begin(9600); // open the serial port pinMode(pwm_a, OUTPUT); //Set control pins to be outputs pinMode(pwm_b, OUTPUT); pinMode(dir_a, OUTPUT); pinMode(dir_b, OUTPUT); pinMode(button, INPUT); // button is an input }
void loop() { val = digitalRead(button); // read input value and store it
//check if there was a transition if ((val == HIGH) && (old_val == LOW)){ state = 1 - state; } old_val = val; // val is now old, store it // check if button is pressed (HIGH) // and chage the state if (state == 1) { rVal = analogRead(righteye); // Read value from right eye sensor lVal = analogRead(lefteye); // Read value from left eye sensor analogWrite(pwm_a, rVal/4); //set both motors to run at one quarter of light level analogWrite(pwm_b, lVal/4); } else { analogWrite(pwm_a, 0); //set both motors to run at one quarter of light level analogWrite(pwm_b, 0); } unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { // save the last time you blinked the LED previousMillis = currentMillis; Serial.println(rVal); Serial.println(lVal); } }
I suppose I could use a similar routine as I use there for the serial output to have the button debouncing on a delay of 100. Would that work?
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 70
Posts: 6802
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #7 on: April 02, 2012, 08:33:45 am » |
You can debounce without using delay() but I'd see if you need to first.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Long Island, NY
Offline
Newbie
Karma: 0
Posts: 39
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
|
 |
« Reply #8 on: April 05, 2012, 01:31:48 am » |
|
|
|
|
|
Logged
|
|
|
|
|
Long Island, NY
Offline
Newbie
Karma: 0
Posts: 39
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
|
 |
« Reply #9 on: April 05, 2012, 01:33:31 am » |
Now I have to add a larger platform above the reads for the core and more sensors, such as IR on the corners.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 70
Posts: 6802
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #10 on: April 05, 2012, 03:05:04 am » |
That looks like it will be a fun thing to work with.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 4
Arduino socks
|
 |
« Reply #11 on: April 13, 2012, 03:42:57 pm » |
What is the body? Some kind of off-the-shelf module?? How much and where could I get one? Looks just the thing I've been looking for?
|
|
|
|
|
Logged
|
|
|
|
|
|
|
|