Advertisement
Guest User

cycle

a guest
Jun 29th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. /*
  2.  * cycle 6 leds up using the gpio
  3.  * a learning lab for loops, goal is to write a loop
  4.  * what was being executed as 6 sepearte commands.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <wiringPi.h>
  11.  
  12. // Define leds
  13. // (wiring Pi pin numbers)
  14.  
  15. #define a       0
  16. #define b       1
  17. #define c       2
  18. #define d       3
  19. #define e       4
  20. #define f       5
  21.  
  22. // The input buttons
  23.  
  24. #define BUTTON1         8
  25. #define BUTTON2         9
  26.  
  27. /*
  28.  * setup:
  29.  * setup the gpio and set up the leds
  30.  *
  31.  */
  32.  
  33. coid setup (void)
  34. {
  35.   int i ;
  36.   if (getuid () !=0)
  37.   {
  38.     fprintf (stderr, "This application needs root priveldges\n" ;
  39.     exit (0) ;
  40.   }
  41.  
  42.   if (wiringPisetup () == -1)
  43.     exit (1) ;
  44.  
  45.    printf ("Setup ... ") ; fflush (stdout) ;
  46.    for (i = 0 ; i < 5 ; ++i)
  47.    {
  48.     pinMode (i, OUTPUT) ;
  49.     digitalWrite (i, 0) ;
  50.    }
  51.  
  52.    pinMode (BUTTON1, INPUT) ;
  53.    PinMode (BUTTON2, INPUT) ;
  54.  
  55.    printf ("OK\n") ;
  56.  
  57.  
  58. /* cycle leds up
  59.  * this will run a loop and add 1 to an integer. The loop will turn off
  60.  * the last led and then turn on the next led.
  61.  */
  62.  
  63.  
  64. void cycleup (void)
  65. {
  66.  int i ;
  67.  
  68.  for (i = 0 ; i < 6 ; ++i)
  69.  
  70.  {
  71.    pinMode (1, OUTPUT) ;
  72.    digitalWrite (i, 1) ;
  73.    digitalWrite (i-1,0) ;
  74.  }
  75.  printf ("OK\n) ;
  76. }
  77.  
  78.  
  79. /*
  80. *
  81. * main
  82. * call setup and then loop
  83. */
  84.  
  85. int main (void)
  86. {
  87.  setup () ;
  88.  for (;;)
  89.  {
  90.   cycleup () ;
  91.  }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement