RASBERRY PICO

mail@pastecode.io avatar
unknown
plain_text
2 years ago
10 kB
7
Indexable
#include <Servo.h>
#include "SoftwareSerial.h"
#include "HX711.h"
#define calibration_factor 7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define LOADCELL_DOUT_PIN  11
#define LOADCELL_SCK_PIN  12
 SoftwareSerial gsmsertext(2,3); // RX, TX

const int pingalu = 4; // Trigger Pin of Ultrasonic Sensor
const int echoalu = 5; // Echo Pin of Ultrasonic Sensor

const int pingplastic = 6; // Trigger Pin of Ultrasonic Sensor
const int echoplastic = 7; // Echo Pin of Ultrasonic Sensor
String bin1="0";
String bin2="0";

String cellnumber="+639264968828";
String textmsg="SYSTEM READY";
HX711 scale;

int pointalu=0;
int pointplastic=0;
String inputString = "";  
int cm1=0;
int cm2=0;
int pd=500;
int pointalu1=0;
int pointplastic1=0;
int serialnumber1=0;
int serialnumber2=0;
int serialnumber3=0;
SoftwareSerial myprinter(19,20); // RX, TX

Servo myservo;  // create servo object to control a servo
Servo dropservo;  // create servo object to control a servo
Servo lid;  // create servo object to control a servo




const int irPin = 17;     // the number of the pushbutton pin// variables will change:
int irState = 0;         // variable for reading the pushbutton status
String serdata="000000000";
const int inductivePin = 16;     // the number of the pushbutton pin// variables will change:
int inductiveState = 0;         // variable for reading the pushbutton status
int indind=0;
int irind=0;

int ident=0;
int startmark=0;
String time1="10:12am";
String time2="12/16/2022";

void gsmsms()
{
  
  
  gsmsertext.println("ATE1\r\n");
  Serial.println("sms1");
  delay(1000);
  gsmsertext.println("AT+CMGF=1\r\n");
  Serial.println("sms2");
  delay(1000);
  gsmsertext.print("AT+CMGS=\""+ cellnumber +"\"\r\n");
  Serial.println("sms3");
  delay(1000);  
  gsmsertext.print(" ALUMINUM BIN: "+ String(bin1) +"% / PLASTIC BIN: "+ String(cm2)+"%");
  Serial.println("sms4");
  
  delay(3500);
  gsmsertext.write(0x1A);
  Serial.println("sms5");
  delay(3000); 
  Serial.println("sms snt p");
  }
void sneddatatorpi()
{
//alu  

      serialnumber1=random(100,999);
     serialnumber2=random(200,599);
     serialnumber3=random(300,499);
       serdata=String(serialnumber1) +""+String(serialnumber2)+""+String(serialnumber3);

checkbin();

Serial.println(serdata+"R"+String(pointalu1)+"S"+String(pointalu)+"T"+String(pointplastic1)+"U"+String(pointplastic)+"V"+String(cm1)+"W"+String(cm2)+"X"+String(time1)+"Y"+String(time2));

//TOTAL
//pointalu1+pointplastic1
//pointalu+pointplastic
  }
void closelid()
{   lid.attach(18);
    lid.write(180);
    delay(1500);
    lid.detach();
   }
void openlid()
{   lid.attach(18);
    lid.write(0);
  myservo.attach(15);
     myservo.write(90);
  
    
    delay(1500);
    lid.detach();   }

void printrecepit()
{
  
  
 
  myprinter.println("                                ");delay(pd);
  myprinter.println("     REVERSE VENDING MACHINE    ");delay(pd);
  myprinter.println(" BARANGAY 310, STA. CRUZ  MANILA");delay(pd);
  myprinter.println("                                ");delay(pd);
  myprinter.println("--------------------------------");delay(pd);
  myprinter.println("        OFFICIAL RECEIPT        ");delay(pd);
  myprinter.println("--------------------------------");delay(pd);
    myprinter.print("DATE:");myprinter.println(time2);delay(pd);
    myprinter.print("TIME:");myprinter.println(time1);delay(pd);
    myprinter.print("SN:");myprinter.println(serdata);delay(pd);
  myprinter.println("LOCATION:MAIN                       ");delay(pd);
  myprinter.println("--------------------------------");delay(pd);
  myprinter.println("             ITEM/S             ");delay(pd);
  myprinter.println("================================");delay(pd);
  myprinter.println("                QTY    POINTS   ");delay(pd);
    myprinter.print("METAL           ");myprinter.print(pointalu1);myprinter.print("       ");myprinter.println(pointalu);delay(pd);
    myprinter.print("PLASTIC         ");myprinter.print(pointplastic1);myprinter.print("       ");myprinter.println(pointplastic);delay(pd);
  myprinter.println("================================");delay(pd);
    myprinter.print("TOTAL:          ");myprinter.print(pointalu1+pointplastic1);myprinter.print("       ");myprinter.println(pointalu+pointplastic);delay(pd);
  myprinter.println("================================");delay(pd);
  myprinter.println("    THANK YOU FOR RECYCLING     ");delay(pd);
  myprinter.println("                                ");delay(pd);
  myprinter.println("                                ");delay(pd);
  myprinter.println("                                ");delay(pd);
 Serial.println("%");
  pointalu1=0;
  pointalu=0;
  pointplastic1=0;
  pointplastic=0;
  }

void checkbin()
{
   long duration, inches, cm;
   pinMode(pingplastic, OUTPUT);
   digitalWrite(pingplastic, LOW);
   delayMicroseconds(2);
   digitalWrite(pingplastic, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingplastic, LOW);
   pinMode(echoplastic, INPUT);
   duration = pulseIn(echoplastic, HIGH);
   inches = microsecondsToInches(duration);
   cm1 = microsecondsToCentimeters(duration);
  
   delay(100);
   
   pinMode(pingalu, OUTPUT);
   digitalWrite(pingalu, LOW);
   delayMicroseconds(2);
   digitalWrite(pingalu, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingalu, LOW);
   pinMode(echoalu, INPUT);
   duration = pulseIn(echoalu, HIGH);
   inches = microsecondsToInches(duration);
   cm2 = microsecondsToCentimeters(duration);
   
   
   delay(100);

}


long microsecondsToInches(long microseconds) {return microseconds / 74 / 2;}
long microsecondsToCentimeters(long microseconds) {return microseconds / 29 / 2;}


void setup() {
 
   myprinter.begin(9600);
    gsmsertext.begin(9600);
   Serial.begin(9600);
 Serial.println("Start");
  // printrecepit();
   dropservo.attach(14);  
   dropservo.write(10);
   myservo.attach(15);
   
   myservo.write(90);
     
   delay(2000);
   dropservo.detach();    
   pinMode(inductivePin, INPUT);
   pinMode(irPin, INPUT_PULLUP);
     scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  inputString.reserve(200);

}

void loop() {
  

  
  irState = digitalRead(irPin);
  inductiveState = digitalRead(inductivePin);
 
  if (inductiveState == LOW) {indind=1;}
  if (inductiveState == HIGH) {indind=0;}
  if (irState == LOW) {irind=1;}
  if (irState == HIGH) {irind=0;}
  if (irind==0)
  { closelid();
   for (int thisPin = 0; thisPin < 1000; thisPin++) 
       {delay(1); inductiveState = digitalRead(inductivePin);}
    if (inductiveState == LOW) {indind=1;}
    if (inductiveState == HIGH) {indind=0;}
    if (indind==1)
    {//Aluminum    
       int data1=scale.get_units();
       Serial.println(data1);
       if (data1<5){pointalu=pointalu+1; pointalu1=pointalu1+1; ident=1;}
       if (data1>4){ ident=3;}
    }

  if (indind==0)
     {//plastic
     int data1=scale.get_units();
     Serial.println(data1);
     if (data1<15){pointplastic=pointplastic+1;pointplastic1=pointplastic1+1; ident=2;}
     if (data1>14){if (data1<19) {pointplastic=pointplastic+2;pointplastic1=pointplastic1+1; ident=2;}if(data1>18){ ident=3;}}
     }
  }  

  if (ident==1)
    {
    Serial.println("!");
    myservo.attach(15);  
    myservo.write(180);
    delay(2000);
    //drop
    dropservo.attach(14);  
    dropservo.write(180);     
    delay(2000);
    dropservo.write(10);     
    delay(2000);
    dropservo.detach();    
    ////////////
    myservo.write(90);
    //delay(2000);
     sneddatatorpi();
    ident=0;
    myservo.detach(); 
    closelid();
    scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
    scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
   
    }
  

   if (ident==2){
    Serial.println("@");
    myservo.attach(15);  
    myservo.write(30);
    delay(2500);
    //drop
    dropservo.attach(14);  
    dropservo.write(180);     
    delay(2000);
    dropservo.write(10);     
    delay(2000);
    dropservo.detach();     
   ////////////
   myservo.write(90);
  // delay(2000);
 sneddatatorpi();
   ident=0;
   myservo.detach(); 
   closelid();
   scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
   scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
 //  sneddatatorpi();
  }


  if (ident==3)
   {//drop
   ident=0;
   Serial.println("#");
   dropservo.attach(14);  
   dropservo.write(160);     
   delay(2500);
   dropservo.write(10);     
   delay(2000);
   dropservo.detach();    
   scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
   scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
   sneddatatorpi();
   ////////////
  }
}




void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar == 'A') {printrecepit();}
    if (inChar == 'B') {openlid();}
    if (inChar == 'C') {sneddatatorpi(); gsmsms();}
//T12:12:22amt12/12/2022d100g100h+639123456789i
    //time
    if (inChar == 't') {time1=inputString;  inputString = "";Serial.println(time1);}
    //date
    if (inChar == 'd') {time2=inputString;time2.replace('t', ' ');time2.trim();inputString = "";Serial.println(time2);}
    //bin1
    if (inChar == 'g') {bin1=inputString;bin1.replace('d', ' ');bin1.trim();inputString = "";Serial.println(bin1);}
    //bin2
    if (inChar == 'h') {bin2=inputString;bin2.replace('g', ' ');bin2.trim();inputString = "";Serial.println(bin2);}
    //number to text then send sms
    if (inChar == 'i') {cellnumber=inputString;cellnumber.replace('h', ' ');cellnumber.trim();inputString = "";startmark=0;Serial.println(cellnumber);
    //send sms
     gsmsms();
    
    
    }



    
    if (startmark==1){inputString += inChar;}
    if (inChar == 'T') {startmark=1;}


    
  }
}