Go Down

Topic: My First Programme (Read 635 times) previous topic - next topic

Onions

Hi all! After moving up to a new school and taking robotics as an option,
I thought I would impress my teacher by building a simple robot for the
first lesson (tee hee!) ;D. With the exception of playing around with the
onboard LED I have never programmed it before though, so, after
scrolling through the examples I added, adapted, written and produced
my own program based on the digital example "button". Here it is:


*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int relayPin =  12;      // the number of the motor reversing pin
const int motorPin =  8;      // the number of the turning pin
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the motor pin as an output:
 pinMode(motorPin, OUTPUT);
// initialize the relay pin as an output:
 pinMode(relayPin, OUTPUT);    
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);    
}

void loop(){
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {    
   // turn reversing pin on:    
   digitalWrite(relayPin, HIGH);
  delay(500);          
 //wait half a second
  digitalWrite(relayPin, LOW);
 //sends robot forwards
   digitalWrite(motorPin, HIGH);
 //turns robot
   delay(500);
 //pause half a second
   digitalWrite(motorPin, LOW);
 //sends robot forward again
 }
 else {
   // turn LED off:
   digitalWrite(relayPin, LOW);
   digitalWrite(motorPin, LOW);
 }
}



What I was wondering is quite simple: will it work? The idea starts off
similar to the schmatic shown in the example, using a micro swith
instead of the tactile switch shown. If the robot drives into the wall it
closes the switch. This then activates a DPDT relay wired up as an
always on H-bridge and reverses the motors. After a set period of
time one motor goes forward again as the relay pin goes low, then
motor pin turns one of the motors off, causing the robot to turn. The
motor pin then goes low and the robot sets off in a different direction
to before untill it hits a wall and the whole process starts again. Thats
the idea anyway. Will it work though?

Thanks for your time, Onions.




My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) :D

mowcius

Quote
and taking robotics as an option

You must go to a pretty wicked school, I wish I could have taken robotics...

77evester

Yeah... me too...

Well done :)

Go Up