Sunday, November 9, 2014

Hi!

Its been a long time since I last posted in this Blog. In this post, I'll be sharing with you a simple code for arduino uno that you can play around with. This codes are used with the Leds. Basically, a few modifications have been done from the built-in blink code. The setup of my Leds are as follow.
The Green, Yellow, Red and Red Leds have been wired to Arduino pin 4,3,2 and 1 respectively. The negative side of the Leds are common ground to Arduino Ground Pin.



With the usage of simple "for"" loop function, it is possible to make each of the led blink multiple times individually. For example, with this code: 

Beginning of code: Copy from below onwards.

int count;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 4,1,2,3 as an output.
  pinMode(4, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
    for(count=0;count<3;count++)
    {
    digitalWrite(1, HIGH);   // turn the extreme right (first) led on
    delay(250);              
    digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    delay(250);              
    };
    
    for(count=0;count<10;count++)
    {
    digitalWrite(2, HIGH);   // turn the second LED on (HIGH is the voltage level)
    delay(50);              
    digitalWrite(2, LOW);    // turn the LED off by making the voltage LOW
    delay(50);      
    }      
    
    for(count=0;count<5;count++)
    {
    digitalWrite(3, HIGH);   // turn the third LED on (HIGH is the voltage level)
    delay(100);              
    digitalWrite(3, LOW);    // turn the LED off by making the voltage LOW
    delay(100);      
    }

  digitalWrite(4, HIGH);   // turn the final LED on (HIGH is the voltage level)
  delay(100);              
  digitalWrite(4, LOW);    // turn the LED off by making the voltage LOW
  delay(100);      
}

End of code:Copy till above this line.

You can make the Leds blink as such (starts at 00;02) :


video

By comparing between the Leds, you can easily tell that the number of blinks, duration of each blink and the speed of each blink is different.

Another example of code will be this:
                                              Beginning of code: Copy from below onwards.
int count;

void setup(){
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  }
  
 void loop(){
    digitalWrite(1,HIGH);     //Test start
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    delay(1000);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);      //Test end
    
    for(count=0;count<3;count++)
    {
    digitalWrite(1,HIGH);     //Sweep left start
    delay(50);
    digitalWrite(1,LOW);
    digitalWrite(2,HIGH);
    delay(50);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    delay(50);
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
    delay(50);
    digitalWrite(4,LOW);      //sweep left end
    
    digitalWrite(4,HIGH);     //Sweep right start
    delay(50);
    digitalWrite(4,LOW);
    digitalWrite(3,HIGH);
    delay(50);
    digitalWrite(3,LOW);
    digitalWrite(2,HIGH);
    delay(50);
    digitalWrite(2,LOW);
    digitalWrite(1,HIGH);
    delay(50);
    digitalWrite(1,LOW);      //sweep left end
    }
    
    for(count=0;count<20;count++)
    {
    digitalWrite(1,HIGH);     //Blink
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    delay(20);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);      //Blink
    delay(20);
    }
    
    
    for(count=0;count<5;count++)
    {
    digitalWrite(1,HIGH);    //both end start
    digitalWrite(4,HIGH);
    delay(250);
    }
    digitalWrite(1,LOW);
    digitalWrite(4,LOW);     //both end end

    for(count=0;count<5;count++)
    {
    digitalWrite(2,HIGH);    //center on start
    digitalWrite(3,HIGH);
    delay(250);
    }
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);     //center on end
    
    for(count=0;count<5;count++)
    {
    digitalWrite(2,HIGH);    //center blink start
    digitalWrite(3,HIGH);
    delay(100);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    delay(100);              //center blink end
    }
    delay(1000);                                                                                  
      
    digitalWrite(4,HIGH);    //move right start
    delay(500);
    digitalWrite(4,LOW);
    digitalWrite(3,HIGH);
    delay(500);
    digitalWrite(3,LOW);    
    digitalWrite(2,HIGH);    //move right end
    digitalWrite(1,HIGH);    //opponent on
    delay(250);
    
    digitalWrite(1,HIGH);    //opponent alive
    
    for(count=0;count<5;count++)
    {
    digitalWrite(2,HIGH);    //attacker dying start
    delay(100);
    digitalWrite(2,LOW);     //attacker dying end
    delay(100);
    }    
    digitalWrite(2,LOW);
    
    digitalWrite(1,HIGH);    //move left start
    delay(500);
    digitalWrite(1,LOW);
    digitalWrite(2,HIGH);
    delay(500);
    digitalWrite(2,LOW);    
    digitalWrite(3,HIGH);    //move left end

    digitalWrite(4,HIGH);    //opponent on
    delay(250);
    
    for(count=0;count<5;count++)
    {
    digitalWrite(3,HIGH);
    delay(100);
    digitalWrite(3,LOW);
    delay(100);  
    }
    
    digitalWrite(4,HIGH);    //opponent survive
    delay(250);
    digitalWrite(4,LOW);
    digitalWrite(3,HIGH);
    delay(250);
    digitalWrite(3,LOW);
    digitalWrite(2,HIGH);
    delay(250);
    
    for(count=0;count<10;count++)
    {
    digitalWrite(2,HIGH);
    delay(100);
    digitalWrite(2,LOW);
    delay(100);
    }
    
    for(count=0;count<10;count++)
    {
    digitalWrite(2,HIGH);
    digitalWrite(1,HIGH);
    digitalWrite(3,HIGH);
    delay(100);
    digitalWrite(2,LOW);
    digitalWrite(1,LOW);
    digitalWrite(3,LOW);
    delay(100);
    }
    
     for(count=0;count<10;count++)
    {
    digitalWrite(2,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(1,HIGH);
    digitalWrite(3,HIGH);
    delay(100);
    digitalWrite(2,LOW);
    digitalWrite(1,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    delay(100);
    }
    
    delay(1000);
  }
    End of code:Copy till above this line.

With the code above, you can make the Leds blink as such (starts at 00;14) : 


video

That is all for now. Enjoy changing the delay and pattern of the Leds. Cheers!

No comments:

Post a Comment