Week 4 - Workshop and Lecture

This week we modified and used a different code to get 6 LED to light up in a sequence.

We managed to do this succesfuly,



We also managed to control the speed of the LED sequence via a potentiometer,



It was not too difficult too adapt the code to have it working with the potentiometer,


int potPin = 2; // select the input pin for the potentiometer
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
int num_pins = 6; // the number of pins (i.e. the length of the array)
int val = 0; // variable to store the value coming from the sensor

void setup()
{
int i;

for (i = 0; i < num_pins; i++) // the array elements are numbered from 0 to num_pins - 1
pinMode(pins[i], OUTPUT); // set each pin as an output
Serial.begin(9600);
}


void loop()
{
int i;

for (i = 0; i < num_pins; i++) { // loop through each pin...
val = analogRead(potPin);
digitalWrite(pins[i], HIGH); // turning it on,
delay(val+50); // pausing,
digitalWrite(pins[i], LOW); // and turning it off.
Serial.println(val);
}
for (i = num_pins - 1; i >= 0; i--) {
val = analogRead(potPin);
digitalWrite(pins[i], HIGH);
delay(val+50);
digitalWrite(pins[i], LOW);
Serial.println(val);
}
}



Pictures


No comments: