Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. void setup(){
  2. pinMode(red,OUTPUT);
  3. pinMode(yellow,OUTPUT);
  4. pinMode(green,OUTPUT);
  5. }
  6. That was easy. Now for the difficult part – the actual logic of a traffic light. I’m going to create a separate function for changing the lights, and you’ll see why later.
  7.  
  8. When you first begin programming, the code itself is very rudimentary – it’s figuring out the minute logic details that presents the biggest problem. The key to being a good programmer is to be able to look at any process, and break it down into its fundamental steps.
  9.  
  10. void loop(){
  11. changeLights();
  12. delay(15000);
  13. }
  14.  
  15. void changeLights(){
  16. // green off, yellow for 3 seconds
  17. digitalWrite(green,HIGH);
  18. digitalWrite(yellow,LOW);
  19. delay(3000);
  20.  
  21. // turn off yellow, then turn red on for 5 seconds
  22. digitalWrite(yellow,LOW);
  23. digitalWrite(red,HIGH);
  24. delay(5000);
  25.  
  26. // red and yellow on for 2 seconds (red is already on though)
  27. digitalWrite(yellow,HIGH);
  28. delay(2000);
  29.  
  30. // turn off red and yellow, then turn on green
  31. digitalWrite(yellow,LOW);
  32. digitalWrite(red,LOW);
  33. digitalWrite(green,HIGH);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement