dhiltonp

hexbright loop test

Dec 13th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. #include <math.h>
  2. #include <Wire.h>
  3.  
  4.  
  5. // Settings
  6. #define OVERTEMP 340
  7. // Pin assignments
  8. #define DPIN_RLED_SW 2
  9. #define DPIN_GLED 5
  10. #define DPIN_PWR 8
  11. #define DPIN_DRV_MODE 9
  12. #define DPIN_DRV_EN 10
  13. #define APIN_TEMP 0
  14. #define APIN_CHARGE 3
  15.  
  16.  
  17.  
  18. boolean on = 0;
  19. unsigned long last_time = 0;
  20. float avg_loop_time = 0;
  21. int print_countdown = 50;
  22.  
  23. void setup()
  24. {
  25.   // We just powered on! That means either we got plugged
  26.   // into USB, or the user is pressing the power button.
  27.   pinMode(DPIN_PWR, INPUT);
  28.   digitalWrite(DPIN_PWR, LOW);
  29.  
  30.   // Initialize GPIO
  31.   pinMode(DPIN_RLED_SW, INPUT);
  32.   pinMode(DPIN_GLED, OUTPUT);
  33.   pinMode(DPIN_DRV_MODE, OUTPUT);
  34.   pinMode(DPIN_DRV_EN, OUTPUT);
  35.   digitalWrite(DPIN_DRV_MODE, LOW);
  36.   digitalWrite(DPIN_DRV_EN, LOW);
  37.  
  38.   // Initialize serial busses
  39.   Serial.begin(9600);
  40.   Wire.begin();
  41.  
  42.   last_time = millis();
  43.  
  44.   Serial.println("Powered up!");
  45. }
  46.  
  47.  
  48. void loop()
  49. {
  50.   unsigned long time = millis();
  51.   //blink
  52.   if (!on) {
  53.     last_time = time;
  54.     on = 1;
  55.     analogRead(APIN_CHARGE);
  56.     Serial.println("running");
  57.     pinMode(DPIN_PWR, OUTPUT);
  58.     digitalWrite(DPIN_PWR, HIGH);
  59.     digitalWrite(DPIN_DRV_MODE, LOW);
  60.     analogWrite(DPIN_DRV_EN, 64);
  61.   }
  62.   else {
  63.    
  64.     {// time passed *(int, so 2 is between 2 and 3)
  65. //    Serial.println(time-last_time);  // returns values from 2-5
  66.     }
  67.  
  68.     {    // avg time passed    
  69.       if(!print_countdown) {
  70.         Serial.println(avg_loop_time);  // stabilizes between 6 and 6.5 with print_countdown = 1, at 50, stabilizes at .1 or so.
  71.         print_countdown = 50;
  72.       }
  73.       print_countdown --;
  74.       avg_loop_time = (avg_loop_time*50.0 + (time-last_time))/51;
  75.     }
  76.     last_time=time;
  77.   }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment