Oladon

RP Hack-a-thon take 2 billion

Jun 16th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.18 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(100);
  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.       StopTakePhotoCmd();
  93.       delay(500);
  94.       SendResetCmd();
  95.       delay(500);
  96. }
  97.  
  98. if (photocellReading>=coffeeThreshold && isCoffeeBeingMade){
  99.   //coffee is still in progress of being made
  100.   //add the time we slept to the time it is taking to make the coffee
  101.   coffeeTimer = coffeeTimer + delayTime;
  102.   digitalWrite(13, HIGH);
  103. }
  104.  
  105. if (photocellReading<coffeeThreshold && isCoffeeBeingMade){
  106.   //coffee is done being made
  107.  
  108.   //write the interval to the serial port for python to pick it up
  109.   coffeeTimer = coffeeTimer + delayTime;
  110.   Serial.print("C0FFEE: ");
  111.   Serial.println(coffeeTimer/1000);
  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