In: Computer Science
Write a C program to blink two LEDs connected to Raspberry pi. One must blink at a rate of 1 Hz and the other at a rate of 2 HZ.
Here our task is to blink 2 Led at rate of 1 KHz and 2 KHz
In order program this in raspberry pi we have to include wiring.pi libraray to Raspberry Pi .After successful installation of the library file you can code it in any available C IDE such as geany etc..
Things to consider
delay= 1 frequency
For first led
delay=1 1000 Hz = 1ms
so on time = 0.5 ms =500 micro seconds
off time =0.5 ms =500 micro seconds
For led 2
delay=1 2000 Hz = 0.5ms
so on time = 0.25 ms =250 micro seconds
off time =0.25ms =250 micro seconds
CODE
(please read all comments for better understanding of the program)
I am also attching a text version of the code in case you need to copy paste
#include <wiringPi.h> //including wiringpi file from
library
#include <stdio.h>
#define Led1 0 // led which blink at 1 KHz rate at pin 0
#define Led2 1 // led which blink at 2 KHz rate at pin 1
int main() {
pinMode(Led, OUTPUT);
while(1) { // a loop having delay of 1 millisecond (1 millisec=1000
micro sec)
digitalWrite(Led1, HIGH); //turning on led 1 at beginning of
loop
digitalWrite(Led2, HIGH); //turning on led 2 at beginning of
loop
delayMicroseconds(250); //delay of 250
microseconds
digitalWrite(Led2, LOW); //led2 off (2khz led)
delayMicroseconds(250);
digitalWrite(Led1, LOW); //turning on off led 1 ( so it blink 1
times in loop (1 ms))
digitalWrite(Led2, HIGH); //led 2 on again
delayMicroseconds(250);
digitalWrite(Led2, LOW); //led 2 off (so that it blink 2 times in a
loop(1 ms)
delayMicroseconds(250);
}
return 0;
}