Monday, January 12, 2009

I have finished my cell bot today. I have made a code to make three groups of lights light up in sequence. Here is the code.
int pins[] = {13,12,11,};
int num_pins = 3;

void setup()
{
int i;
for (i = 0; i < num_pins; i++)
pinMode(pins[i], OUTPUT);
}
void loop()
{
int i;
for (i = 0; i < num_pins; i++)
{
digitalWrite(pins[i], HIGH);
delay(250);
digitalWrite(pins[i], LOW);
}
for (i = num_pins - 1; i >= 0; i--)
{
digitalWrite(pins[i], HIGH);
delay(250);
digitalWrite(pins[i], LOW);
}
}
Next I made a code with a push button that controls a stirp of lights.
int ledPin = 9;
int inputPin = 2;
int val = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
}
void loop()
{
val = digitalRead(inputPin);
if (val == HIGH)
{
digitalWrite(ledPin, HIGH);
delay(5000);
}
else
{
digitalWrite(ledPin, LOW);
}
}
lastly I made a code to control my motors. They go forward then when you press a button it goes backwards. It also blinks a light 3 time befor it moves.
int switchPin = 2;
int motor1Pin = 3;
int motor2Pin = 4;
int speedPin = 7;
int ledPin = 13;
void setup()
{
pinMode(switchPin, INPUT);
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(speedPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(speedPin, HIGH);
blink(ledPin, 3, 500);
}
void loop()
{
if (digitalRead(switchPin) == HIGH)
{
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
delay(5000);
}
else
{
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
}
}
void blink(int whatPin, int howManyTimes, int milliSecs)
{
int i = 0;
for ( i = 0; i < howManyTimes; i++)
{
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}

1 comment: