Showing posts with label driving. Show all posts
Showing posts with label driving. Show all posts
Wednesday, February 4, 2015
Robot Club - fourth session completed
Link to Preparing post details in the parts in the kits.: http://winkleink.blogspot.co.uk/2014/11/preparing-for-2015-after-school.html
Link to First session; http://winkleink.blogspot.co.uk/2015/01/robot-club-first-session-completed.html
Note; I did not post for the second session. There is a short summary in the Third Session post.
Link to Third session: http://winkleink.blogspot.co.uk/2015/01/robot-club-third-session-completed.html
This week we nearly had a full house.
Only 1 child missing due to illness.
The 4 who attended the previous week remember a lot of their work and code so were getting stuck into getting their robots to go around a corner.
I had a bit more fun getting drivers on enough school PCs to permit the Arduino Nano to be seen. It needs custom USB/Serial drivers that are not included by default.
The teacher who is ICT coordinator came in to see the session in full swing with robots spinning, or moving along the floor. With coding going on and due to plus and minus lines on the batteries being shorted a nice little display of black smoke and the lovely smell of burnt plastic.
No real harm done just a jumper wires plastic sleeving was melted.
I congratulated the child on making smoke as it's something we all do when we work with electronics. They were kind of shocked.
It was great that the teacher had the same attitude so no harm done and no concern about burning down the school.
I tested their robot kit and Arduino. Nothing damaged, amazingly so did a little bit of rewiring and handed them back their robot to get working on again.
On of the kids who was in the previous session had a problem where their robot was pulling to the left. These are small cheap kits so the motors aren't tuned and the wheels are small and have grip meaning they catch on the carpet. Meaning they cannot reliably go straight.
We tried to fix it by turning the wheels in or our to change the resistance but no go so introduced that child to analogWrite.
A value of 235 seemed to make it all work perfectly. They had a nice straight running robot.
Once again the kids were not that eager to leave as they were perfecting their code and making it better.
At the end one of the girls who was being collected wanted to show her mother what she had done so she gave a great demonstration of her robot. Holding down reset to stop it moving.
Set it up and let it go. She then explained the code she had made as well as giving details on some of the other parts we will be using.
Next week we will be using an input device. A small line following sensor. I will mark the edge of the room area with white paper providing a contrast with the blue flooring. The idea will be to detect the paper and make the robot turn.
This will be the first time the kids do inputs and will also require the introduction of the 'if' statement.
Looking forward to another productive session next Monday.
Sunday, May 8, 2011
Arduino 8x8 LED driving game
8x8 LED matrix used for a driving game.
There's a single LED at the bottom that is the 'car'
The track is random and moving from the top to the bottom.
The car is controlled by a potentiometer (pot)
Below is the code. Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges. It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.
There's a single LED at the bottom that is the 'car'
The track is random and moving from the top to the bottom.
The car is controlled by a potentiometer (pot)
Below is the code. Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges. It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.
// Control an 8x8 LED display with 2 x 74HC595 shift registers
// Using only 3 pins from the Arduino
// Random Number - used to decide if to go back 1, stay the same line or forward 1
int randNumber;
//currentLine is used to record the current position of the track
int currentLine = 3;
// Postion of car
int carPosition = 3;
// Arduino Pin for the Pot used as the controller input for the car
int potPin = 2;
// Reading for the pot
int potRead;
// Range for the Pot as full travel would be too much to be practical
int potLow;
int potHigh;
//Pin connected to Pin 12 of 74HC595 (Latch)
int latchPin = 8;
//Pin connected to Pin 11 of 74HC595 (Clock)
int clockPin = 12;
//Pin connected to Pin 14 of 74HC595 (Data)
int dataPin = 11;
uint8_t led[8];
uint8_t car[8];
uint8_t trackLine[5];
long counter1 = 0;
void setup() {
// Seed Random Generator with noise from analog pin 0
randomSeed(analogRead(0));
//set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// Set Track to straight
led[0] = B11100011;
led[1] = B11100011;
led[2] = B11100011;
led[3] = B11100011;
led[4] = B11100011;
led[5] = B11100011;
led[6] = B11100011;
led[7] = B11100011;
// Set car position - looks backward, but the display is LSB so a 1
// on the right will show as a 1 on the left of the screen
car[0] = B00000001;
car[1] = B00000010;
car[2] = B00000100;
car[3] = B00001000;
car[4] = B00010000;
car[5] = B00100000;
car[6] = B01000000;
car[7] = B10000000;
// used to set the track decision
trackLine[0] = B10001111;
trackLine[1] = B11000111;
trackLine[2] = B11100011;
trackLine[3] = B11110001;
// trackLine[4] = B00001001;
potRead = analogRead(potPin);
potLow = potRead - 70;
potHigh = potRead + 89;
}
void loop() {
// counter1 used for delay in animation
counter1++;
// set the LEDs
screenUpdate();
potRead = analogRead(potPin);
if (potRead > potHigh){
potRead = potHigh;}
if (potRead < potLow){
potRead = potLow;}
carPosition = (potRead - potLow)/20;
// Loop for the action
if (counter1 >=75) {
counter1 = 0;
// Do track
for (int i = 0 ; i < 7 ; i++){
led[i] = led[i+1];
}
// check of the car has crashed if it has then do a crash signal
if (led[0] & car[carPosition]){
crash();
}
led[0] = led[0] | car[carPosition];
randNumber = random(3)-1;
currentLine = currentLine +randNumber;
if (currentLine == 0){
currentLine = 1;}
if (currentLine == 5){
currentLine = 4;}
led[7] = trackLine[currentLine-1];
}
}
void screenUpdate() {
uint8_t row = B00000001;
for (byte k = 0; k < 9; k++) {
// Open up the latch ready to receive data
digitalWrite(latchPin, LOW);
shiftIt(~row );
shiftIt(led[k] ); // LED array
// Close the latch, sending the data in the registers out to the matrix
digitalWrite(latchPin, HIGH);
row = row << 1;
}
}
void shiftIt(byte dataOut) {
// Shift out 8 bits LSB first,
// on rising edge of clock
boolean pinState;
//clear shift register read for sending data
digitalWrite(dataPin, LOW);
// for each bit in dataOut send out a bit
for (int i=0; i<8; i++) {
//set clockPin to LOW prior to sending bit
digitalWrite(clockPin, LOW);
// if the value of DataOut and (logical AND) a bitmask
// are true, set pinState to 1 (HIGH)
if ( dataOut & (1<<i) ) {
pinState = HIGH;
}
else {
pinState = LOW;
}
//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);
//send bit out on rising edge of clock
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
}
//stop shifting
digitalWrite(clockPin, LOW);
}
void crash(){
// Used to set all bits on or off
int allBits = B11111111;
// Do 10 loop so of on and off
for (int show=0; show <10; show++){
for (int i=0; i <8; i++){
led[i] = allBits;
}
for (int delaylots=0; delaylots<200; delaylots++){
screenUpdate();
}
if(allBits == B11111111){
allBits = B00000000;
}
else
{
allBits = B11111111;
}
}
// Reset the position of the car to the middle of the track
potRead = analogRead(potPin);
potLow = potRead - 70;
potHigh = potRead + 89;
for (int i =0; i <8; i++){
led[i] = trackLine[2];
}
}
Subscribe to:
Posts (Atom)