Wednesday, February 5, 2014

Voltage level indicator using Arduino

Instruction:
  1. User changes value of a pot resistor. Reference voltage is 5 v.
  2. Arduino reads the input and converts it as following. 0V is will display a 0 and 5v will display 9 on the seven-segment display.
Design:

Code:
/*Name: Voltage level indicator
potentiometer wiper (middle terminal) connected to analog pin 3
outside leads to ground and +5V
variable to store the value read
*/
int val = 0;          
int analogPin = A0;   
int ret;

int pin_2 = 2;
int pin_3 = 3;
int pin_4 = 4;
int pin_5 = 5;
int pin_6 = 6;
int pin_7 = 7;
int pin_8 = 8;

void displayZero() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, LOW);
}

void displayOne() {
   digitalWrite(pin_2, LOW);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, LOW);
}

void displayTwo() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, LOW);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, HIGH);
}

void displayThree() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, HIGH); 
}

void displayFour() {
   digitalWrite(pin_2, LOW);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayFive() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displaySix() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displaySeven() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, LOW);
}

void displayEight() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayNine() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void Dec_to_7seg(int x) {
    switch(x) {
       case 0:
            displayZero();
            break;
       case 1:
            displayOne();
            break;
       case 2:
            displayTwo();
            break;
       case 3:
            displayThree();
            break;
       case 4:
            displayFour();
            break;
       case 5:
            displayFive();
            break;
       case 6:
            displaySix();
            break;
       case 7:
            displaySeven();
            break;
       case 8:
            displayEight();
            break;
       case 9:
            displayNine();
            break;
    }
}

void setup(){
  pinMode(pin_2, OUTPUT);
  pinMode(pin_3, OUTPUT);
  pinMode(pin_4, OUTPUT);
  pinMode(pin_5, OUTPUT);
  pinMode(pin_6, OUTPUT);
  pinMode(pin_7, OUTPUT);
  pinMode(pin_8, OUTPUT);
  Serial.begin(9600);          //  setup serial
}

void loop(){
  val = analogRead(analogPin);    // read the input pin
  float voltage = val * (9 / 1023.0);
  ret = (int)voltage; 
  //Serial.println(ret);
  Dec_to_7seg(ret);
}

Make a binary 4 bit counter using Arduino

Instruction:
  1. L.E.Ds act as binary 1 and 0 (LED ON=1; LED OFF= 0). 
  2. Write a code that will continuously count from 0 to max. Each number will be displayed for 2 seconds.
Design:
binary 4 bit counter
 Code:
/*
Name: 4 bit counter.
*/

int led_1 = 1; //0th bit led pin
int led_2 = 2; //1st bit led pin
int led_3 = 3; //2nd bit led pin
int led_4 = 4; //3rd bit led pin

void setup() {
  //setup the pinmode as output
  pinMode(led_1, OUTPUT);
  pinMode(led_2, OUTPUT);
  pinMode(led_3, OUTPUT);
  pinMode(led_4, OUTPUT);
}

void loop() {
  //loop from 0 to max
  for(int i=0; i<=15; i++) {
    //switch-case for 0-max
    switch(i) {
     case 0:
        digitalWrite(led_1, LOW);
        digitalWrite(led_2, LOW);
        digitalWrite(led_3, LOW);
        digitalWrite(led_4, LOW);
      break;
     case 1:
        digitalWrite(led_1, HIGH);
        digitalWrite(led_2, LOW);
        digitalWrite(led_3, LOW);
        digitalWrite(led_4, LOW);
      break;
     case 2:
        digitalWrite(led_1, LOW);
        digitalWrite(led_2, HIGH);
        digitalWrite(led_3, LOW);
        digitalWrite(led_4, LOW);
      break;
     case 3:
        digitalWrite(led_1, HIGH);
        digitalWrite(led_2, HIGH);
        digitalWrite(led_3, LOW);
        digitalWrite(led_4, LOW);
      break;
     case 4:
        digitalWrite(led_1, LOW);
        digitalWrite(led_2, LOW);
        digitalWrite(led_3, HIGH);
        digitalWrite(led_4, LOW);
        break;
     case 5:
        digitalWrite(led_1, HIGH);
        digitalWrite(led_2, LOW);
        digitalWrite(led_3, HIGH);
        digitalWrite(led_4, LOW);
        break;
     case 6:
        digitalWrite(led_1, LOW);
        digitalWrite(led_2, HIGH);
        digitalWrite(led_3, HIGH);
        digitalWrite(led_4, LOW);
      break;
     case 7:
        digitalWrite(led_1, HIGH);
        digitalWrite(led_2, HIGH);
        digitalWrite(led_3, HIGH);
        digitalWrite(led_4, LOW);
        break;
     case 8:
        digitalWrite(led_1, LOW);
        digitalWrite(led_2, LOW);
        digitalWrite(led_3, LOW);
        digitalWrite(led_4, HIGH);
        break;
     case 9:
        digitalWrite(led_1, HIGH);
        digitalWrite(led_2, LOW);
        digitalWrite(led_3, LOW);
        digitalWrite(led_4, HIGH);
        break;
     case 10:
        digitalWrite(led_1, LOW);
        digitalWrite(led_2, HIGH);
        digitalWrite(led_3, LOW);
        digitalWrite(led_4, HIGH);
        break;
     case 11:
        digitalWrite(led_1, HIGH);
        digitalWrite(led_2, HIGH);
        digitalWrite(led_3, LOW);
        digitalWrite(led_4, HIGH);
        break;
     case 12:
        digitalWrite(led_1, LOW);
        digitalWrite(led_2, LOW);
        digitalWrite(led_3, HIGH);
        digitalWrite(led_4, HIGH);
        break;
     case 13:
        digitalWrite(led_1, HIGH);
        digitalWrite(led_2, LOW);
        digitalWrite(led_3, HIGH);
        digitalWrite(led_4, HIGH);
        break;
     case 14:
        digitalWrite(led_1, LOW);
        digitalWrite(led_2, HIGH);
        digitalWrite(led_3, HIGH);
        digitalWrite(led_4, HIGH);
        break;
     case 15:
        digitalWrite(led_1, HIGH);
        digitalWrite(led_2, HIGH);
        digitalWrite(led_3, HIGH);
        digitalWrite(led_4, HIGH);
        break;
     default:
        //reset at default
        digitalWrite(led_1, LOW);
        digitalWrite(led_2, LOW);
        digitalWrite(led_3, LOW);
        digitalWrite(led_4, LOW);
        break;
    }
    delay(2000); //Each number will be displayed for 2 seconds.
  }
}

Make a Switch Counter using Arduino

Instruction:
  1. A push-button is connected to pin number 2. 
  2. Write a program that will count the number of time the button is pressed. Whenever a button is pressed the program will send the count including the latest one. That is the program will send the count if and only if button is pressed.
Design:
switch counter using arduino

Code:
/*
Name: Switch Counter
*/
int pin_2 = 2;  //button pin             
int buttonState; //button state
int val;                       
int cnt = 0;  //button press counter       

void setup() {
  pinMode(pin_2, INPUT);  //setup button pin as input  
  Serial.begin(9600); //initialize serial         
  buttonState = digitalRead(pin_2);   //read the button state
}


void loop(){
  val = digitalRead(pin_2);      //read the button state
  if (val != buttonState) {         
    if (val == HIGH) {               
      cnt++; //increment button counter             
      Serial.println(cnt); //print the value of button press counter
    }
  }
  buttonState = val;                
}

Naive Decimal to Hex Converter using Arduino

Instruction:
  1. User types decimal number (0-15) in serial monitor.
  2. Arduino reads the input and shows the corresponding hexadecimal number using Single seven segment display.
  3. If user types a out of range decimal value, Arduino will handle that by flashing “E” on the seven-segment display and an error message in serial monitor.
Design:
Naive Decimal to Hex Converter
Code:
/*
Name: Naive Decimal to Hex Converter
*/
int pin_2 = 2;
int pin_3 = 3;
int pin_4 = 4;
int pin_5 = 5;
int pin_6 = 6;
int pin_7 = 7;
int pin_8 = 8;
String string = "";       //initialize string 

void displayZero() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, LOW);
}

void displayOne() {
   digitalWrite(pin_2, LOW);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, LOW);
}

void displayTwo() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, LOW);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, HIGH);
}

void displayThree() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, HIGH); 
}

void displayFour() {
   digitalWrite(pin_2, LOW);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayFive() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displaySix() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displaySeven() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, LOW);
}

void displayEight() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayNine() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayA() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayB() {
   digitalWrite(pin_2, LOW);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayC() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, LOW);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, LOW);
}

void displayD() {
   digitalWrite(pin_2, LOW);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, HIGH);
}

void displayE() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, LOW);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayF() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, LOW);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void setup() {   
  //set pin 2 to 8 as output 
  pinMode(pin_2, OUTPUT);
  pinMode(pin_3, OUTPUT);
  pinMode(pin_4, OUTPUT);
  pinMode(pin_5, OUTPUT);
  pinMode(pin_6, OUTPUT);
  pinMode(pin_7, OUTPUT);
  pinMode(pin_8, OUTPUT);
  Serial.begin(9600); //initialize serial
}

void loop() {
    if(Serial.available() > 0){
        char ch = (char) Serial.read();
        if(ch == '\r') {
            if(string == "0") {
               displayZero();
            } else if(string == "1") {
               displayOne();
            } else if(string == "2") {
               displayTwo();
            } else if(string == "3") {
               displayThree();
            } else if(string == "4") {
               displayFour();
            } else if(string == "5") {
               displayFive();
            } else if(string == "6") {
               displaySix();
            } else if(string == "7") {
               displaySeven();
            } else if(string == "8") {
               displayEight();
            } else if(string == "9") {
               displayNine();
            } else if(string == "10") {
               displayA();
            } else if(string == "11") {
               displayB();
            } else if(string == "12") {
               displayC();
            } else if(string == "13") {
               displayD();
            } else if(string == "14") {
               displayE();
            } else if(string == "15") {
               displayF();
            } else {
               displayE();
               Serial.println("NUMBER IS OUT OF RANGE");
            }
            string = "";
        } else {
            string += ch;
        }
    }
}

Make a Digital Dice using Arduino

Instruction:
  1. User press a input switch to start the dice.
  2. The dice displays 1-6 randomly (for 100 milliseconds) through a 7-segment display.
  3. If user press the input switch again it will stop at the current random
    number and display the number
  4. When input switch is pushed once again it will repeat the process from step 1 to 3.
Design:

Code:
/*
Name: Digital Dice
*/
int pin_2 = 2;
int pin_3 = 3;
int pin_4 = 4;
int pin_5 = 5;
int pin_6 = 6;
int pin_7 = 7;
int pin_8 = 8;
int pin_9 = 9;
int buttonPin = 1;
int buttonState;
int val;
int randNumber;
int cnt = 0;

void displayOne() {
   digitalWrite(pin_2, LOW);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, LOW);
}

void displayTwo() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, LOW);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, HIGH);
}

void displayThree() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, LOW);
   digitalWrite(pin_8, HIGH);  
}

void displayFour() {
   digitalWrite(pin_2, LOW);
   digitalWrite(pin_3, HIGH);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, LOW);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displayFive() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, LOW);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void displaySix() {
   digitalWrite(pin_2, HIGH);
   digitalWrite(pin_3, LOW);
   digitalWrite(pin_4, HIGH);
   digitalWrite(pin_5, HIGH);
   digitalWrite(pin_6, HIGH);
   digitalWrite(pin_7, HIGH);
   digitalWrite(pin_8, HIGH);
}

void setup() {            
    pinMode(buttonPin, INPUT);   
    pinMode(pin_2, OUTPUT); 
    pinMode(pin_3, OUTPUT);
    pinMode(pin_4, OUTPUT);
    pinMode(pin_5, OUTPUT);
    pinMode(pin_6, OUTPUT);
    pinMode(pin_7, OUTPUT);
    pinMode(pin_8, OUTPUT);
    pinMode(pin_9, OUTPUT);
    digitalWrite(9, 0);  // start with the "dot" off
    Serial.begin(9600);
    randomSeed(analogRead(0));
}

void randomVisit() { 
    randNumber = random(1, 7);
    switch(randNumber) {
        case 1:
           displayOne();
           break; 
        case 2:
           displayTwo();
           break;
        case 3:
           displayThree();
           break; 
        case 4:
           displayFour();
           break;
        case 5:
           displayFive();
           break; 
        case 6:
           displaySix();
           break;      
    }
    delay(100);
}

void loop() {
    val = digitalRead(buttonPin);
    if(cnt % 2 == 1) {
        randomVisit(); 
    }
    if(val != buttonState) {
        if(val == HIGH) {
            cnt++;
            if(cnt % 2 == 1) {
                randomVisit(); 
            }
        } 
    }
    buttonState = val;
}