Thursday, May 19, 2011

LED Bar Graph




















/*
LED bar graph
Turns on a series of LEDs based on the value of an analog sensor.
This is a simple way to make a bar graph display. Though this graph
uses 10 LEDs, you can use any number by changing the LED count
and the pins in the array.
This method can be used to control any series of digital outputs that
depends on an analog input.
The circuit:
* LEDs from pins 2 through 9 to ground
created 4 Sep 2010
by Tom Igoe
**************************************
**************************************
Experiment performed by:
Sulav Timsina(sulav7155@yahoo.com)
Sudip Shrestha(shrestha.sp11@gmail.com)
**************************************
**************************************
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BarGraph
*/


// these constants won't change:
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 10; // the number of LEDs in the bar graph

int ledPins[] = {
2, 3, 4, 5, 6, 7,8,9 }; // an array of pin numbers to which LEDs are attached


void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}

void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}

Wednesday, May 18, 2011

Simple 1 bit 7-segment LED counter


/*
counter
created 2011/05/18
Sudip Shrestha
Sulav Timsina
*/
int count=0;
int delay_val=1000;

// The setcountup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);

}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
switch (count) {
case 0:
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
delay(delay_val);

//do something when var equals 1
break;
case 1:
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(delay_val);
//do something when var equals 2
break;
case 2:
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
delay(delay_val);
//do something when var equals 2
break;
case 3:
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
delay(delay_val);
//do something when var equals 2
break;
case 4:
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(delay_val);
//do something when var equals 2
break;
case 5:
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(delay_val);
//do something when var equals 2
break;
case 6:
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(delay_val);
//do something when var equals 2
break;
case 7:
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(delay_val);
//do something when var equals 2
break;
case 8:
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(delay_val);
//do something when var equals 2
break;
case 9:
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(delay_val);
//do something when var equals 2
break;
}

if (count > 9)
{
count=0;
//action A
}
else
{
count=count+1;
// action B
}
}