Oladon

RP Hack-a-thon Coffee Snooper

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