Advertisement
Guest User

Untitled

a guest
Dec 11th, 2011
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.65 KB | None | 0 0
  1.  
  2. #include <Servo.h>
  3.  
  4. #define LIGHT_TRESHOLD 7
  5.  
  6. #define CMD_STEP_FORWARD "f"
  7. #define CMD_STEP_REWIND "r"
  8. #define CMD_FAST_STEP_FORWARD "F"
  9. #define CMD_FAST_STEP_REWIND "R"
  10. #define CMD_FULL_REWIND "#"
  11.  
  12. #define CMD_SET_SPEED "S"
  13. #define CMD_SET_TIC_LENGTH "T"
  14. #define CMD_SET_TIC_MULTIPLIER "X"
  15.  
  16. #define CMD_CONFIRM_CONNECTION "V"
  17. #define CMD_READ_BRIGHTNESS "L"
  18. #define CMD_READ_IS_LOCKED "C"
  19.  
  20. #define CMD_MEMORY "M"
  21.  
  22. #define RESPONSE_SUN_AVAILABLE "READY TO BURN"
  23. #define RESPONSE_SUN_DIMMED "NIGHT"
  24.  
  25. #define RESPONSE_SUN_POS "POS="
  26. #define RESPONSE_SUN_FF "FF="
  27. #define RESPONSE_SUN_REW "REW="
  28. #define RESPONSE_SUN_BRIGHTNESS "LIGHT="
  29.  
  30. #define RESPONSE_PING "!"
  31.  
  32. #define RESPONSE_FLASH "FLASH"
  33.  
  34. uint8_t * heapptr, * stackptr;
  35. void check_mem() {
  36.   stackptr = (uint8_t *)malloc(4);          // use stackptr temporarily
  37.   heapptr = stackptr;                     // save value of heap pointer
  38.   free(stackptr);      // free up the memory again (sets stackptr to 0)
  39.   stackptr =  (uint8_t *)(SP);           // save value of stack pointer
  40. }
  41.  
  42.  
  43. Servo servo1;
  44. String buffer;
  45. String feedBuffer;
  46.  
  47. int cfg_base = 93;
  48. int cfg_tic = 20;
  49. int cfg_tic_x = 1;
  50. int cfg_speed = 10;
  51.  
  52. int cfg_delay = 300;
  53.  
  54. int ff = 0;
  55. int rew = 0;
  56.  
  57. int pos = 0;
  58.  
  59. int servoPin = 9;
  60. int lockPin = 10;
  61. int brightnessPin = 14;
  62. int failPin = 12;
  63.  
  64. unsigned long lightTimer = 0;
  65. int lightness = 1023;
  66. int lightnessCheckPeriod = 1000;
  67. int roll = 0;
  68.  
  69. unsigned long pingTimerPeriod = 30000;
  70. unsigned long pingTimer = 0;
  71. unsigned long pingLEDTimer = 0;
  72.  
  73. unsigned long fullMillis = 17179868*2;
  74.  
  75. unsigned long fixedMillis() {
  76.   return millisRollover()*fullMillis + millis();
  77. }
  78.  
  79. int millisRollover() {
  80.   // get the current millis() value for how long the microcontroller has been running
  81.   //
  82.   // To avoid any possiblity of missing the rollover, we use a boolean toggle that gets flipped
  83.   //   off any time during the first half of the total millis period and
  84.   //   then on during the second half of the total millis period.
  85.   // This would work even if the function were only run once every 4.5 hours, though typically,
  86.   //   the function should be called as frequently as possible to capture the actual moment of rollover.
  87.   // The rollover counter is good for over 35 years of runtime. --Rob Faludi http://rob.faludi.com
  88.   //
  89.   static int numRollovers=0; // variable that permanently holds the number of rollovers since startup
  90.   static boolean readyToRoll = false; // tracks whether we've made it halfway to rollover
  91.   unsigned long now = millis(); // the time right now
  92.   unsigned long halfwayMillis = 17179868; // this is halfway to the max millis value (17179868)
  93.  
  94.   if (now > halfwayMillis) { // as long as the value is greater than halfway to the max
  95.     readyToRoll = true; // you are ready to roll over
  96.   }
  97.  
  98.   if (readyToRoll == true && now < halfwayMillis) {
  99.     // if we've previously made it to halfway
  100.     // and the current millis() value is now _less_ than the halfway mark
  101.     // then we have rolled over
  102.     numRollovers = numRollovers++; // add one to the count the number of rollovers
  103.     readyToRoll = false; // we're no longer past halfway
  104.   }
  105.   return numRollovers;
  106. }
  107.  
  108. int stringToInt(String s)
  109.      {
  110.            int n;
  111.            char carray[6];
  112.            s.toCharArray(carray, sizeof(carray));
  113.            n = atoi(carray);
  114.            return n;
  115.      }
  116.        
  117. void setup() {
  118.   Serial.begin(28800);
  119.  
  120.   pinMode(lockPin,INPUT); /* DO NOT CHANGE TO OUTPUT!! */
  121.   /*digitalWrite(lockPin, HIGH);*/
  122.  
  123.   pinMode(brightnessPin, INPUT);
  124.  
  125.   pinMode(failPin, OUTPUT);
  126.  
  127.   servo1.attach(servoPin);
  128.   buffer = "";
  129.   feedBuffer = "";
  130.   pingTimer = fixedMillis();
  131.   pingLEDTimer = fixedMillis();
  132.  
  133.   digitalWrite(failPin, LOW);
  134.   Serial.println("HI "+String(fixedMillis()));
  135. }
  136. void processCommand(String buffer) {
  137.    /* Confirm We Are The Sun */
  138.    if(buffer.substring(0,1)==CMD_CONFIRM_CONNECTION) {
  139.      say(RESPONSE_SUN_AVAILABLE);
  140.    }
  141.    
  142.    /* Internal configuration */
  143.    if(buffer.substring(0,1)==CMD_SET_SPEED) {
  144.      updateSpeed(buffer.substring(1));
  145.    }    
  146.    if(buffer.substring(0,1)==CMD_SET_TIC_LENGTH) {
  147.      updateTic(buffer.substring(1));
  148.    }
  149.    if(buffer.substring(0,1)==CMD_SET_TIC_MULTIPLIER) {
  150.      updateTicX(buffer.substring(1));
  151.    }
  152.    
  153.    /* */
  154.    if(buffer.substring(0,1)==CMD_READ_BRIGHTNESS) {
  155.      readBrightness();
  156.    }  
  157.  
  158.    if(buffer.substring(0,1)==CMD_FULL_REWIND) {
  159.      rewind();
  160.    }
  161.    
  162.    if(buffer.substring(0,1)==CMD_STEP_FORWARD) {
  163.      stepForward(cfg_tic, cfg_delay);
  164.    }
  165.    if(buffer.substring(0,1)==CMD_FAST_STEP_FORWARD) {
  166.      stepForward(cfg_tic*cfg_tic_x, 0);
  167.    }
  168.    if(buffer.substring(0,1)==CMD_STEP_REWIND) {
  169.      stepRew(cfg_tic, cfg_delay);
  170.    }
  171.    if(buffer.substring(0,1)==CMD_MEMORY) {
  172.      check_mem();
  173.      say("Memory "+String((long)heapptr)+" "+String((long)stackptr));
  174.    }
  175.    if(buffer.substring(0,1)==CMD_FAST_STEP_REWIND) {
  176.      stepRew(cfg_tic*cfg_tic_x, 0);
  177.    }
  178.    if(buffer.substring(0,1)==RESPONSE_PING) {
  179.      pingLEDTimer = fixedMillis();
  180.      digitalWrite(failPin, LOW);
  181.    }
  182. }
  183.  
  184. boolean isLocked() {
  185.   return digitalRead(lockPin) == HIGH;
  186. }
  187.  
  188. void say(String s) {
  189.   feedBuffer+=s+"\r\n";
  190. }
  191.  
  192. void feed() {
  193.   if(Serial.available()==0) { /* input queue empty */
  194.     Serial.print(feedBuffer.substring(0,1));
  195.     feedBuffer = feedBuffer.substring(1);
  196.   }
  197. }
  198.  
  199. void rewind() {
  200.   servo1.write(cfg_base+(cfg_speed));
  201.  
  202.   int steps = 140;
  203.  
  204.   while(steps>0 && !isLocked()) {
  205.     steps--;
  206.     delay(cfg_tic);
  207.   }
  208.  
  209.   servo1.write(cfg_base-cfg_speed);
  210.  
  211.   delay(cfg_tic*2);
  212.   steps = 8;
  213.   while(steps>0 && isLocked()) {
  214.     servo1.write(cfg_base-cfg_speed);
  215.     steps--;
  216.     delay(cfg_tic);
  217.     servo1.write(cfg_base);
  218.     delay(100);
  219.   }
  220.  
  221.   servo1.write(cfg_base);
  222.   pos = 0;
  223.   say(RESPONSE_SUN_DIMMED);
  224. }
  225.  
  226. void readBrightness() {
  227.   int brightness = analogRead(brightnessPin);
  228.   say(RESPONSE_SUN_BRIGHTNESS+String(brightness));
  229. }
  230. void updateSpeed(String sp) {
  231.   cfg_speed = stringToInt(sp);
  232.   say("Speed = "+String(cfg_speed));
  233. }
  234.  
  235. void updateTic(String sp) {
  236.   cfg_tic = stringToInt(sp);
  237.   say("Step = "+String(cfg_tic));
  238. }
  239.  
  240. void updateTicX(String sp) {
  241.   cfg_tic_x = stringToInt(sp);
  242.   say("Step X = "+String(cfg_tic_x));
  243. }
  244.  
  245. void stepForward(int length, int pause) {
  246.   servo1.write(cfg_base-cfg_speed);
  247.   delay(length);
  248.   servo1.write(cfg_base);  
  249.   ff++;  
  250.   pos++;
  251.   say(RESPONSE_SUN_POS+String(pos));  
  252.  
  253.   delay(pause);
  254. }
  255.  
  256. void stepRew(int length, int pause) {
  257.   if(isLocked()) {
  258.     say(RESPONSE_SUN_DIMMED);
  259.     pos = 0;
  260.     return;
  261.   }
  262.   servo1.write(cfg_base+cfg_speed);
  263.   delay(length);
  264.   servo1.write(cfg_base);
  265.   rew++;  
  266.   pos--;
  267.   say(RESPONSE_SUN_POS+String(pos));
  268.   //Serial.println(RESPONSE_SUN_REW+String(rew));
  269.   delay(pause);  
  270. }
  271.  
  272. void loop() {
  273.  
  274.  
  275.  
  276.   if ( Serial.available()) {
  277.     char ch = Serial.read();    
  278.     if((ch!='\r')&&(ch!='\n')) {
  279.       buffer += ch;
  280.     }
  281.     if(ch=='\n') {
  282.       processCommand(buffer);
  283.       buffer="";
  284.     }    
  285.   }
  286.  
  287.   if(fixedMillis() - lightTimer>lightnessCheckPeriod) {
  288.     lightTimer = fixedMillis();
  289.     int newLightness = analogRead(brightnessPin);
  290.     if(newLightness-lightness>LIGHT_TRESHOLD) {
  291.       say(RESPONSE_FLASH);
  292.     }
  293.     lightness = newLightness;
  294.   }
  295.   if(fixedMillis() - pingTimer>pingTimerPeriod) {
  296.     pingTimer = fixedMillis();
  297.     say(RESPONSE_PING);
  298.   }
  299.   if(fixedMillis() - pingLEDTimer>pingTimerPeriod*2) {
  300.     pingLEDTimer = fixedMillis();
  301.     digitalWrite(failPin, HIGH);
  302.   }
  303.   feed();
  304.  
  305.   if(roll<millisRollover()) {
  306.     say("ROLL "+roll);
  307.     roll=millisRollover();
  308.   }
  309.  
  310.  
  311. }
  312.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement