Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Photocell simple testing sketch.
- Connect one end of the photocell to 5V, the other end to Analog 0.
- Then connect one end of a 10K resistor from Analog 0 to ground
- Connect LED from pin 11 through a resistor to ground
- For more information see www.ladyada.net/learn/sensors/cds.html */
- #include <NewSoftSerial.h>
- int photocellPin = 0; // the cell and 10K pulldown are connected to a0
- int photocellReading; // the analog reading from the sensor divider
- //int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
- //int LEDbrightness; //
- boolean isCoffeeBeingMade=false;
- int delayTime=500;
- int coffeeThreshold = 600;
- long int coffeeTimer = 0;
- byte incomingbyte;
- NewSoftSerial mySerial(4,5); //Configure pin 4 and 5 as soft serial port
- int a=0x0000,j=0,k=0,count=0; //Read Starting address
- uint8_t MH,ML;
- boolean EndFlag=0;
- void SendResetCmd();
- void SendTakePhotoCmd();
- void SendReadDataCmd();
- void StopTakePhotoCmd();
- void setup(void) {
- // We'll send debugging information via the Serial monitor
- Serial.begin(19200);
- mySerial.begin(38400);
- pinMode(13, OUTPUT);
- SendResetCmd();
- delay(5000);
- }
- void loop(void) {
- photocellReading = analogRead(photocellPin);
- //Serial.print("Analog reading = ");
- Serial.println(photocellReading); // the raw analog reading
- if (photocellReading>=coffeeThreshold && !isCoffeeBeingMade){
- //Someone must have started making coffee
- digitalWrite(13, HIGH);
- //Start the timer
- isCoffeeBeingMade=true;
- //Take a picture
- SendTakePhotoCmd();
- while(mySerial.available()>0)
- {
- incomingbyte=mySerial.read();
- }
- byte a[32];
- while(!EndFlag)
- {
- j=0;
- k=0;
- count=0;
- SendReadDataCmd();
- delay(100);
- while(mySerial.available()>0)
- {
- incomingbyte=mySerial.read();
- k++;
- if((k>5)&&(j<32)&&(!EndFlag))
- {
- a[j]=incomingbyte;
- if((a[j-1]==0xFF)&&(a[j]==0xD9)) //Check if the picture is over
- EndFlag=1;
- j++;
- count++;
- }
- }
- for(j=0;j<count;j++)
- { if(a[j]<0x10)
- Serial.print("0");
- Serial.print(a[j],HEX);
- Serial.print(" ");
- } //Send jpeg picture over the serial port
- Serial.println();
- }
- StopTakePhotoCmd();
- delay(500);
- SendResetCmd();
- delay(500);
- }
- if (photocellReading>=coffeeThreshold && isCoffeeBeingMade){
- //coffee is still in progress of being made
- //add the time we slept to the time it is taking to make the coffee
- coffeeTimer = coffeeTimer + delayTime;
- digitalWrite(13, HIGH);
- }
- if (photocellReading<coffeeThreshold && isCoffeeBeingMade){
- //coffee is done being made
- //write the interval to the serial port for python to pick it up
- coffeeTimer = coffeeTimer + delayTime;
- Serial.print("C0FFEE: ");
- Serial.println(coffeeTimer/1000);
- digitalWrite(13, LOW); // set the LED off
- delay(100);
- digitalWrite(13, HIGH);
- delay(100);
- digitalWrite(13, LOW);
- delay(100);
- digitalWrite(13, HIGH);
- delay(100);
- digitalWrite(13, LOW);
- //reset the variables
- isCoffeeBeingMade=false;
- coffeeTimer=0;
- }
- // LED gets brighter the darker it is at the sensor
- // that means we have to -invert- the reading from 0-1023 back to 1023-0
- //photocellReading = 1023 - photocellReading;
- //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
- //LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
- //analogWrite(LEDpin, LEDbrightness);
- delay(delayTime);
- }
- //Send Reset command
- void SendResetCmd()
- {
- mySerial.print(0x56, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(0x26, BYTE);
- mySerial.print(0x00, BYTE);
- }
- //Send take picture command
- void SendTakePhotoCmd()
- {
- mySerial.print(0x56, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(0x36, BYTE);
- mySerial.print(0x01, BYTE);
- mySerial.print(0x00, BYTE);
- }
- //Read data
- void SendReadDataCmd()
- {
- MH=a/0x100;
- ML=a%0x100;
- mySerial.print(0x56, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(0x32, BYTE);
- mySerial.print(0x0c, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(0x0a, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(MH, BYTE);
- mySerial.print(ML, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(0x20, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(0x0a, BYTE);
- a+=0x20; //address increases 32£¬set according to buffer size
- }
- void StopTakePhotoCmd()
- {
- mySerial.print(0x56, BYTE);
- mySerial.print(0x00, BYTE);
- mySerial.print(0x36, BYTE);
- mySerial.print(0x01, BYTE);
- mySerial.print(0x03, BYTE);
- }
Add Comment
Please, Sign In to add comment