A smart trashcan

So right now, we have a project to develop a smart trashcan. Which will be composed of two ultrasonic sensor, RGB led light, Servo and RFID. I was able to make three separate programs. First is for the opening of the lid of the trash can with an ultrasonic and servo, second the other ultrasonic and LED light to let the person to monitor if the bin is full (red if full, green is not), and lastly the RFID and the servo so that once the bin is full the ultrasonic(that will open the bin) wont work because its full, instead the rfid will be the one to open it to change then plasti inside the bin. Right now I’m stuck with this code. I’m not a pro in programming

#include <Servo.h> //
#include <SPI.h> //
#include <MFRC522.h>
#define RST_PIN         9
#define SS_PIN          10
//#define trigpin 2
//#define echopin 3
//#define R 7
//#define G 8
//#define B 9
int trigPins = 4;
int echoPins = 5;
long distance;
long duration;
MFRC522 mfrc522(SS_PIN, RST_PIN);  //3.3 Create MFRC522 instance
Servo servo1;
//5V

void setup() {
  servo1.attach(6);
  pinMode(trigPins, OUTPUT);
  pinMode(echoPins, INPUT);
 // pinMode(trigpin, OUTPUT); //set trigpin as output
 // pinMode(echopin, INPUT); //set echopin as input
 // pinMode(R, OUTPUT); // set R,G and B as outputs
 // pinMode(G, OUTPUT);
 // pinMode(B, OUTPUT);

  Serial.begin(9600); // Initialize serial communications with the PC

  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)

  SPI.begin();      // Init SPI bus

  mfrc522.PCD_Init();   // Init MFRC522

  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details

  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));

}

void loop() {
  // put your main code here, to run repeatedly:


  Serial.println(distance);
  delay(50);
  ultra();
  servo1.write(0);
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  if (distance <= 20 ||  mfrc522.uid.uidByte[0] == 0xD2 &&  mfrc522.uid.uidByte[1] == 0xD9 && mfrc522.uid.uidByte[2] == 0x79 && mfrc522.uid.uidByte[3] == 0x0E)
  {
    Serial.println("Authorized Access");
    servo1.write(90);
    delay(1000);
  }
}
void ultra() {
  digitalWrite(trigPins, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPins, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPins, LOW);
  duration = pulseIn(echoPins, HIGH);
  distance = duration * 0.034 / 2;
}

The RFID works, while the ultrasonic does not. If i try it in separate programs they work but somehow they can’t when merged, there’s no error message or anything hope for help

long distance;
long duration;

and

distance = duration * 0.034 / 2;

will not work.
These variable need to be of the float type.

If i try it in separate programs they work

No that code will never work.

Grumpy_Mike:

long distance;

long duration;



and


distance = duration * 0.034 / 2;



will not work.
These variable need to be of the float type.
No that code will never work.

nope, it's still not working

Grumpy_Mike:

long distance;

long duration;



and


distance = duration * 0.034 / 2;




will not work.
These variable need to be of the float type.
No that code will never work.

they actually do, and idk why :slight_smile: :slight_smile: :slight_smile:

long distance;
long duration = 12345;

void setup() {
Serial.begin(9600);
distance = duration * 0.034 / 2;
Serial.println(distance);
}

void loop() {
}

Result printed 209
change both longs to float
Result printed 209.87
This is not the same.

if (distance <= 20 ||  mfrc522.uid.uidByte[0] == 0xD2 &&  mfrc522.uid.uidByte[1] == 0xD9 && mfrc522.uid.uidByte[2] == 0x79 && mfrc522.uid.uidByte[3] == 0x0E)

So you want the door to open if the distance is less than 20 OR you have the right card?