Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #define clock_speed 16e6
  2. #define RED_PIN 8
  3. #define YELLOW_PIN 10
  4. #define GREEN_PIN 12
  5.  
  6.  
  7. //define the delays
  8. int red_on = 3000, red_yellow_on = 1000, green_on = 3000, green_blink = 5000, yellow_on = 1000;
  9.  
  10. int delay_timer(int miliseconds){
  11.   int count = 0;
  12.   while(true){
  13.     if(TCNT0 >= clock_speed) {
  14.       TCNT0 = 0;
  15.       count++;
  16.       if(count == miliseconds) {
  17.         count = 0;
  18.         break;
  19.       }
  20.     }
  21.   }
  22.   return 0;
  23. }
  24.  
  25.  
  26. void setup() {
  27.   // put your setup code here, to run once:
  28.   // define the pins connected
  29.   pinMode(RED_PIN, OUTPUT);  
  30.   pinMode(YELLOW_PIN, OUTPUT);
  31.   pinMode(GREEN_PIN, OUTPUT);
  32.   //set up timer
  33.   TCCR0A = 0b00000000;
  34.   TCCR0B = 0b00000101;
  35.   TCNT0 = 0;
  36. }
  37.  
  38. void loop() {
  39.   // put your main code here, to run repeatedly:
  40.   //to make red led on
  41.   digitalWrite(RED_PIN, HIGH);
  42.   delay_timer(red_on);
  43.   //to make yellow led on
  44.   digitalWrite(YELLOW_PIN, HIGH);
  45.   delay_timer(red_yellow_on);
  46.   //turning off red pin and yellow pin
  47.   digitalWrite(RED_PIN, LOW);
  48.   digitalWrite(YELLOW_PIN, LOW);
  49.   //turning green on
  50.   digitalWrite(GREEN_PIN, HIGH);
  51.   delay_timer(green_on);
  52.   //turning green off
  53.   digitalWrite(GREEN_PIN, LOW);
  54.   //for truning green on and off for three times
  55.   for(int i = 0; i < 3; i++) {
  56.     delay_timer(green_blink);
  57.     digitalWrite(GREEN_PIN, HIGH);
  58.     delay_timer(green_blink);
  59.     digitalWrite(GREEN_PIN, LOW);
  60.   }
  61.  
  62.   //for turning on yellow
  63.   digitalWrite(YELLOW_PIN, HIGH);
  64.   delay_timer(yellow_on);
  65.   digitalWrite(YELLOW_PIN, LOW);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement