Okay, Hi. Its my first time using this forum and i'm a 12 year old. i have basic knowledge of arduino and am making a project for a competition. So i need answer to 2 things;
- Can someone please check my code and tell me if it would hypothetically work?
My project is a smart dustbin which uses an arduino mega,ultrasonic sensor and a pir sensor. it basically seeks out garbage and it has 3d printed arms which pick up the garbage. and also please suggest any corrections. here's the code
#include <Servo.h>
Servo armek;
Servo armdo;
long duration2, distance2, BackSensor, FrontSensor;
char data = 0;
int sensor = 2;
int state = LOW;
int val = 0;
int detect ;
int motor1Pin1 = 22;
int motor1Pin2 = 23;
int motor2Pin1 = 24;
int motor2Pin2 = 25;
int motor3Pin1 = 28;
int motor3Pin2 = 29;
int motor4Pin1 = 30;
int motor4Pin2 = 31;
int state2;
int flag = 0;
long duration, dist, average;
long aver[3];
int trigPin1 = 5;
int echoPin1 = 6;
int trigPin2 = 7;
int echoPin2 = 8;
void Forward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void Reverse() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
void Left() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void Right() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void Pick() {
armek.write(150);
armdo.write(150);
}
void Free () {
armek.write(0);
armdo.write(0);
}
void Up() {
digitalWrite(motor3Pin1, HIGH);
digitalWrite(motor3Pin2, LOW);
digitalWrite(motor4Pin1, HIGH);
digitalWrite(motor4Pin2, LOW);
}
void Down() {
digitalWrite(motor3Pin1, LOW);
digitalWrite(motor3Pin2, HIGH);
digitalWrite(motor4Pin1, LOW);
digitalWrite(motor4Pin2, HIGH);
}
void StopWheel() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void StopArm() {
digitalWrite(motor3Pin1, LOW);
digitalWrite(motor3Pin2, LOW);
digitalWrite(motor4Pin1, LOW);
digitalWrite(motor4Pin2, LOW);
}
void setup()
{
pinMode(sensor, INPUT);
pinMode(motor1Pin1, OUTPUT), pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT), pinMode(motor2Pin2, OUTPUT);
pinMode(motor3Pin1, OUTPUT), pinMode(motor3Pin2, OUTPUT);
pinMode(motor4Pin1, OUTPUT), pinMode(motor4Pin2, OUTPUT);
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}
void measure() {
digitalWrite(10, HIGH);
digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin1, LOW);
pinMode(echoPin1, INPUT);
duration = pulseIn(echoPin1, HIGH);
dist = (duration / 2) / 29.1; //obtain distance
}
void SonarSensor(int trigPin, int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration2 = pulseIn(echoPin, HIGH);
distance2 = (duration2 / 2) / 29.1;
}
void Empty()
{
void loop() {
SonarSensor(trigPin1, echoPin1);
FrontSensor = distance2;
SonarSensor(trigPin2, echoPin2);
BackSensor = distance2;
Serial.print(FrontSensor);
Serial.print(" - ");
Serial.print(BackSensor);
Serial.print(" - ");
for (int i = 0; i <= 2; i++) {
measure();
aver[i] = dist;
delay(10);
}
dist = (aver[0] + aver[1] + aver[2]) / 3;
while (dist > 10) {
Forward();
}
if (dist == 10)
StopWheel();
Down();
Pick();
Up();
Free();
Left();
Forward();
if (Serial.read()== "Motion detected!")
{
Empty();
Serial.print(dist);
if (Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data and store it into variable data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n"); //New line
if (data == '1') //Checks whether value of data is equal to 1
for (int i = 0; i > 3; i++) {
Left();
delay (500);
Right();
delay (500);
}
} if (data == '0')
{
StopWheel();
StopArm();
}
val = digitalRead(sensor);
if (val == HIGH)
{
delay(1000);
Right();
delay(1000);
Forward();
delay(1000);
Left();
delay (1000);
Forward();
delay(1000);
Left();
delay(1000);
Forward();
delay(1000);
Right();
delay(1000);
Forward();
}
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH;
}
}
else {
while (dist > 10) {
Forward();
}
}
if (state == HIGH) {
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}
- Also, this was my original question, How do we declare an empty function. i need it coz when my pir detects a human, the us should stop for a while, until the dustbin turns around the human. Please answer and Thank You.