Oladon

RP Hack-a-thon take 2 billion and 1

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