Advertisement
zinch

Arduino led lights with photoresistor

Apr 10th, 2017
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. const int DEFAULT_DELAY_SLICE = 10;
  2. void setup()
  3. {
  4.   pinMode( 13 , OUTPUT);
  5.   pinMode( 12 , OUTPUT);
  6.   pinMode( 11 , OUTPUT);
  7.   pinMode( 10 , OUTPUT);
  8.   Serial.begin(9600);
  9. }
  10.  
  11. void loop()
  12. {
  13.   changeStatusWithDelay(turnOn);
  14.   changeStatusWithDelay(turnOff);  
  15. }
  16.  
  17. void changeStatusWithDelay(void (*f)(int))
  18. {
  19.   int totalPortDelay = 0;
  20.   for (int port = 13; port >= 10; port--)
  21.   {
  22.     f( port );
  23.     while(totalPortDelay < getDelayTime())
  24.     {
  25.       delay( DEFAULT_DELAY_SLICE );
  26.       totalPortDelay += DEFAULT_DELAY_SLICE;
  27.     }
  28.     totalPortDelay = 0;
  29.   }
  30. }
  31.  
  32. void turnOn(int port) { digitalWrite( port , HIGH ); }
  33. void turnOff(int port) { digitalWrite( port , LOW ); }
  34.  
  35. int getDelayTime()
  36. {
  37.   int delayTime = 20;
  38.   int photo_voltage = analogRead(0) ;
  39.   Serial.print("message");
  40.   Serial.print(photo_voltage);
  41.   Serial.println();
  42.   if (photo_voltage > 0)
  43.     delayTime = photo_voltage * 20;
  44.    
  45.   return delayTime;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement