Oladon

Hack-a-thon

Jun 16th, 2011
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.84 KB | None | 0 0
  1. #include <NewSoftSerial.h>
  2. #include <Time.h>
  3.  
  4. int photocellPin = 0; // the cell and 10K pulldown are connected to a0
  5. int photocellReading; // the analog reading from the sensor divider
  6. //int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
  7. //int LEDbrightness; //
  8. boolean isCoffeeBeingMade=false;
  9. int delayTime=500;
  10. int coffeeThreshold = 600;
  11. long int coffeeTimer = 0;
  12. time_t start_time;
  13. time_t end_time;
  14.  
  15. byte incomingbyte;
  16. NewSoftSerial mySerial(4,5);                     //Configure pin 4 and 5 as soft serial port
  17. unsigned int a=0x0000;
  18. int j=0,k=0,count=0;                     //Read Starting address
  19.  
  20. uint8_t MH,ML;
  21. boolean EndFlag=0;
  22.  
  23. void SendResetCmd();
  24. void SendTakePhotoCmd();
  25. void SendReadDataCmd();
  26. void StopTakePhotoCmd();
  27.  
  28. void setup(void) {
  29.   // We'll send debugging information via the Serial monitor
  30.   Serial.begin(19200);
  31.   mySerial.begin(38400);
  32.   pinMode(13, OUTPUT);
  33.  
  34.   SendResetCmd();
  35.   delay(5000);
  36. }
  37.  
  38. void loop(void) {
  39.   photocellReading = analogRead(photocellPin);
  40.   //Serial.print("Analog reading = ");
  41.   Serial.println(photocellReading); // the raw analog reading
  42.  
  43.     if (photocellReading>=coffeeThreshold && !isCoffeeBeingMade){
  44.     //Someone must have started making coffee
  45.     digitalWrite(13, HIGH);
  46.     //Start the timer
  47.     isCoffeeBeingMade=true;
  48.     start_time = now();
  49.  
  50.     //Take a picture
  51.     SendTakePhotoCmd();
  52.  
  53.     while(mySerial.available()>0)
  54.     {
  55.       incomingbyte=mySerial.read();
  56.  
  57.     }  
  58.     byte picBuffer[32];
  59.  
  60.     while(!EndFlag)
  61.     {  
  62.       j=0;
  63.       k=0;
  64.       count=0;
  65.       SendReadDataCmd();
  66.  
  67.       delay(100);
  68.       while(mySerial.available()>0)
  69.       {
  70.         incomingbyte=mySerial.read();
  71.         k++;
  72.         if((k>5)&&(j<32)&&(!EndFlag))
  73.         {
  74.           picBuffer[j]=incomingbyte;
  75.           if((picBuffer[j-1]==0xFF)&&(picBuffer[j]==0xD9))      //Check if the picture is over
  76.             EndFlag=1;
  77.           j++;
  78.           count++;
  79.         }
  80.       }
  81.  
  82.       for(j=0;j<count;j++)
  83.       {  
  84.         if(picBuffer[j]<0x10)
  85.           Serial.print("0");
  86.         Serial.print(picBuffer[j],HEX);
  87.         Serial.print(" ");
  88.       }                                       //Send jpeg picture over the serial port
  89.       Serial.println();
  90.     }
  91.     a=0x0000;
  92.     StopTakePhotoCmd();
  93.     delay(500);
  94.   }
  95.  
  96.   if (photocellReading>=coffeeThreshold && isCoffeeBeingMade){
  97.     //coffee is still in progress of being made
  98.     //add the time we slept to the time it is taking to make the coffee
  99.     coffeeTimer = coffeeTimer + delayTime;
  100.     digitalWrite(13, HIGH);
  101.   }
  102.  
  103.   if (photocellReading<coffeeThreshold && isCoffeeBeingMade){
  104.     //coffee is done being made
  105.     end_time = now();
  106.  
  107.     //write the interval to the serial port for python to pick it up
  108.     coffeeTimer = coffeeTimer + delayTime;
  109.     Serial.print("C0FFEE: ");
  110.     //  Serial.println(coffeeTimer/1000);
  111.     Serial.println(end_time - start_time);
  112.     digitalWrite(13, LOW);    // set the LED off
  113.     delay(100);
  114.     digitalWrite(13, HIGH);  
  115.     delay(100);
  116.     digitalWrite(13, LOW);
  117.     delay(100);
  118.     digitalWrite(13, HIGH);    
  119.     delay(100);
  120.     digitalWrite(13, LOW);
  121.     //reset the variables
  122.     isCoffeeBeingMade=false;
  123.     coffeeTimer=0;
  124.   }
  125.  
  126.  
  127.   // LED gets brighter the darker it is at the sensor
  128.   // that means we have to -invert- the reading from 0-1023 back to 1023-0
  129.   //photocellReading = 1023 - photocellReading;
  130.   //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
  131.   //LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
  132.   //analogWrite(LEDpin, LEDbrightness);
  133.  
  134.   delay(delayTime);
  135. }
  136.  
  137. //Send Reset command
  138. void SendResetCmd()
  139. {
  140.   mySerial.print(0x56, BYTE);
  141.   mySerial.print(0x00, BYTE);
  142.   mySerial.print(0x26, BYTE);
  143.   mySerial.print(0x00, BYTE);
  144. }
  145.  
  146. //Send take picture command
  147. void SendTakePhotoCmd()
  148. {
  149.   mySerial.print(0x56, BYTE);
  150.   mySerial.print(0x00, BYTE);
  151.   mySerial.print(0x36, BYTE);
  152.   mySerial.print(0x01, BYTE);
  153.   mySerial.print(0x00, BYTE);  
  154. }
  155.  
  156. //Read data
  157. void SendReadDataCmd()
  158. {
  159.   MH=a/0x100;
  160.   ML=a%0x100;
  161.   mySerial.print(0x56, BYTE);
  162.   mySerial.print(0x00, BYTE);
  163.   mySerial.print(0x32, BYTE);
  164.   mySerial.print(0x0c, BYTE);
  165.   mySerial.print(0x00, BYTE);
  166.   mySerial.print(0x0a, BYTE);
  167.   mySerial.print(0x00, BYTE);
  168.   mySerial.print(0x00, BYTE);
  169.   mySerial.print(MH, BYTE);
  170.   mySerial.print(ML, BYTE);  
  171.   mySerial.print(0x00, BYTE);
  172.   mySerial.print(0x00, BYTE);
  173.   mySerial.print(0x00, BYTE);
  174.   mySerial.print(0x20, BYTE);
  175.   mySerial.print(0x00, BYTE);  
  176.   mySerial.print(0x0a, BYTE);
  177.   a+=0x20;                            //address increases 32£¬set according to buffer size
  178. }
  179.  
  180. void StopTakePhotoCmd()
  181. {
  182.   mySerial.print(0x56, BYTE);
  183.   mySerial.print(0x00, BYTE);
  184.   mySerial.print(0x36, BYTE);
  185.   mySerial.print(0x01, BYTE);
  186.   mySerial.print(0x03, BYTE);        
  187. }
Add Comment
Please, Sign In to add comment