Sunday, February 28, 2010

New behaviour for blob

video

Ive done this by using a counter (increment) in the code. So the programmed counts how long the switch is on and sends output accordingly.

ARDUINO CODE

// set pin numbers:
const int buttonPin = 7; // the number of the reedswitch pin

int buttonState = 0; // variable for reading the pushbutton status
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;

int i = 0; // the counter

void setup() {
// initialize the reedswitch pin as an input:
pinMode(buttonPin, INPUT);

// initialize the LED pin as an output:
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
Serial.begin(9600);
}

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

// check if the reedswitch is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {

i++; // increase the counter by 1
Serial.println(i);

if ( i < 4){ // if the couter is more than 4
digitalWrite(pin2,HIGH);
delay(1000);
digitalWrite(pin4,HIGH);
delay(1000);}

else if ( i < 9){ // if the couter is more than 9

digitalWrite(pin2, HIGH);
delay(timer);
digitalWrite(pin2, LOW);
delay(timer);

digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);

digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);

digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);

digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);


digitalWrite(pin2, HIGH);
delay(timer);
digitalWrite(pin2, LOW);
delay(timer);

digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);

digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);

digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);


digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);

digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);

digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);}

else if ( i > 10){ // if the couter is more than 10, blob is dead

digitalWrite(pin2, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin5, LOW);}

}

else {

i=0;
// turn LED off:
digitalWrite(pin2, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin5, LOW);

Serial.println(i);

}
}

No comments:

Post a Comment