-
1Step 1
//. Motor driver shield- 2012 Copyright (c) Seeed Technology Inc. // // Original Author: Jimbo.we // Contribution: LG // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // // // Basic motion based on A0 A4 sensor input by Aaron "Floz" Casper // for ScrapTrack // // set up variables // name pins // int pinI1=8;//define I1 interface int pinI2=11;//define I2 interface int speedpinA=9;//enable motor A int pinI3=12;//define I3 interface int pinI4=13;//define I4 interface int speedpinB=10;//enable motor B int speed_A =0;//define the speed of motor_A int speed_B =0;//define the speed of motor_B const int photoCell_A = A4; // light sensor_A pin const int photoCell_B = A0; // light sensor_B pin int sensor_value_A = 0;// zero all our math int sensor_value_B = 0;// variables and sensors int output_value_A = 0; int output_value_B = 0; void setup() { // tell the arduino which pins are input and // which ones are output pinMode(photoCell_A, INPUT); pinMode(photoCell_B, INPUT); pinMode(pinI1,OUTPUT); pinMode(pinI2,OUTPUT); pinMode(speedpinA,OUTPUT); pinMode(pinI3,OUTPUT); pinMode(pinI4,OUTPUT); pinMode(speedpinB,OUTPUT); Serial.begin(57600); } void forward() { //This is really the only movement function used to begin with //it enables the motor to a speed determined by the photocell //this configuration should chase bright light. // analogWrite(speedpinA,speed_A);//input a simulation value to set the speed analogWrite(speedpinB,speed_B); digitalWrite(pinI4,LOW);//turn DC Motor B move clockwise digitalWrite(pinI3,HIGH); digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise digitalWrite(pinI1,HIGH); } void stop() { // shuts the motors down by setting both speed-pins // to LOW digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor. digitalWrite(speedpinB,LOW); delay(1000); } void loop() { // read sensor values to figure motor speeds sensor_value_A= analogRead(photoCell_A); sensor_value_B = analogRead(photoCell_B); // map sensor readings to 0-255 range output_value_A = map(sensor_value_A, 0, 30, 0, 255); output_value_B = map(sensor_value_B, 0, 30, 0, 255); //serial debugging output Serial.print(analogRead(photoCell_A)); Serial.print(" "); Serial.print(output_value_A); Serial.print(" "); Serial.print(analogRead(photoCell_B)); Serial.print(" "); Serial.print(output_value_B); Serial.println(" "); // don't pass values beyond what the motor controller // can handle for speed. if (output_value_A >= 255){ output_value_A = 255; } if (output_value_B >= 255){ output_value_B = 255; } // don't overwork the controllers trying to make tiny movements // chances are they won't turn on the motor enough to move, and // just get hot. if (output_value_A <= 128){ output_value_A = 0; } if (output_value_B <= 128){ output_value_B = 0; } // set the speed pin values for each motor controller speed_A= (output_value_A); speed_B= (output_value_B); //Serial.println(speed_A); // move forward with speed_A and speed_B forward(); delay(100); // delay to help jitter/freakouts }
-
2Step 2
How to build a scraptrack:
Step1 find an RC toy you like, but want to make autonomous.
Step2 buy an Arduino (I like the Uno dev board)
Step3 buy a motor sheild, or build your own.
Step4 wiring!
Step5 programming
Step6 sensors!
Step7 more programming
Step8 frantic cursing at a 7th grade math error that makes the robot run in circles
Step9 even more programming
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.