**My project is an automated sanitizer dispensing device with an ultrasonic sensor (hc sr04) and I'm using Arduino UNO, my motor is i think 5v or 9v, my power source is an 18v battery. I want it so that the motor will only function when an object being detected is 15mm or closer, and stand by if not. I found this code here on project hub:
I moved your topic to an appropriate forum category @carrrllll.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
That is not a schematic, that is just a bunch of photographs and it is useless for telling us what you have made.
See:-
Reading a schematic
Colin's Lab video on reading a schematic
Yes you do, but you actually need to fit it into your circuit in order to be any actual use. As it is we can see no motor drive at all in your photographs.
Here is a similar project code from several years back.
You should be able to get some ideas from this.
//
// https://forum.arduino.cc/t/auto-hand-senitizer/931875
//
//
// Highly suggest the OP use the: N E W P I N G L I B R A R Y
// https://playground.arduino.cc/Code/NewPing/
//
//*********************************************************************************
// Version YY/MM/DD Description
// ======= ======== ===========
// 1.00 21 11 20 Working sketch
//
//
//*********************************************************************************
#define ENABLED true
#define DISABLED false
#define DETECTED true
#define notDETECTED false
#define LEDon HIGH
#define LEDoff LOW
//*********************************************************************************
const byte trigPin = 5;
const byte echoPin = 6;
const byte RMA = 10;
const byte RMB = 11;
const byte detectedLED = 12; //comes on as long as the object is in range
const byte heartbeatLED = 13; //toggles ever 500ms
const byte inRange = 30;
const byte outOfRange = 45;
//******************************
bool objectFlag = notDETECTED;
bool lastObjectFlag = notDETECTED;
unsigned long duration;
unsigned int dist;
unsigned int average;
//array for average
long aver[3];
//******************************
//timing stuff
unsigned long heartbeatMillis;
unsigned long distanceMillis;
unsigned long motorMillis;
unsigned long smMillis;
unsigned long dispenserOnTime = 2000; //2 seconds sanitizer ON time
unsigned long deadTime = 250; //1/4 second
//******************************
//State Machine stuff
enum StateMachine {PowerUP, CheckObject, MotorONtiming, OjectRemoved};
StateMachine mState = PowerUP;
//*********************************************************************************
void setup()
{
Serial.begin(9600);
pinMode(heartbeatLED, OUTPUT);
pinMode(detectedLED, OUTPUT);
pinMode(RMA, OUTPUT);
pinMode(RMB, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
} //END of setup()
//*********************************************************************************
void loop()
{
//***************************************
//is it time to toggle the heartbeat LED ?
if (millis() - heartbeatMillis >= 500)
{
//restart the TIMER
heartbeatMillis = millis();
//toggle LED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//****************************************
//is it time to check for an object ?
if (millis() - distanceMillis >= 500)
{
//restart this TIMER
distanceMillis = millis();
checkForObject();
}
//****************************************
//is it time to check the state Machine ?
if (millis() - smMillis >= 50)
{
//restart the TIMER
smMillis = millis();
checkSM();
}
//****************************************
//other non blocking code goes her
//****************************************
} //END of loop()
//*********************************************************************************
void checkSM()
{
switch (mState)
{
//***********************
case PowerUP:
//next state
mState = CheckObject;
break; //END of case PowerUP:
//***********************
case CheckObject:
//was there a change in state ?
if (lastObjectFlag != objectFlag)
{
//update to the new change
lastObjectFlag = objectFlag;
if (objectFlag == DETECTED)
{
//motor ON
digitalWrite(RMA, LOW);
digitalWrite(RMB, HIGH);
Serial.println("\nSanitizer is coming out. ");
Serial.print("RMA = \t");
Serial.println(digitalRead(RMA));
Serial.print("RMB = \t");
Serial.println(digitalRead(RMB));
Serial.println();
//start the TIMER
motorMillis = millis();
//next state
mState = MotorONtiming;
}
} //END of if(lastObjectFlag != objectFlag)
break; //END of case CheckObject:
//***********************
case MotorONtiming:
//has the TIMER expired ?
if (millis() - motorMillis >= dispenserOnTime)
{
//motor OFF
digitalWrite(RMA, LOW);
digitalWrite(RMB, LOW);
Serial.println("\nWe are finished dispensing sanitizer. ");
Serial.print("RMA = \t");
Serial.println(digitalRead(RMA));
Serial.print("RMB = \t");
Serial.println(digitalRead(RMB));
//next state
mState = OjectRemoved;
} //END of if(millis() - motorMillis >= dispenserOnTime)
break; //END of case MotorONtiming:
//***********************
case OjectRemoved:
//was there a change in state ?
if (lastObjectFlag != objectFlag)
{
//update to the new change
lastObjectFlag = objectFlag;
if (objectFlag == notDETECTED)
{
Serial.println("\nObject has been removed.\n ");
//next state
mState = CheckObject;
}
} //END of if (lastObjectFlag != objectFlag)
break; //END of case OjectRemoved:
} //END of switch. . .case
} //END of checkSM()
//*********************************************************************************
void checkForObject()
{
//get 3 distances
for (int i = 0; i <= 2; i++)
{
measure();
aver[i] = dist;
delay(10);
}
//average the 3 distances
dist = (aver[0] + aver[1] + aver[2]) / 3;
Serial.print("Distance to the object \t= ");
Serial.println(dist);
//****************************************
//is the object in range ?
if (dist <= inRange)
{
objectFlag = DETECTED;
digitalWrite(detectedLED, LEDon);
}
//is the object is out of range
else if (dist >= outOfRange)
{
objectFlag = notDETECTED;
digitalWrite(detectedLED, LEDoff);
}
} //END of checkForObject()
//*********************************************************************************
void measure()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
dist = (duration / 2) / 29.1;
} //END of measure()
//*********************************************************************************
i don't know how to use it, and my motor has to be soldered to the female part of the jumper for it to be plugged to the motor driver, i don't know man, i have 0 knowledge in this
Then you will have to start learning, or are you expecting us to do your homework for you? If yes then this is known as cheating.
Yes we will help you learn, but I for one will not be party to cheating. I used to be a University Lecturer in the UK (in the U.S. this is known as a professor), so I know what cheating is.
Odd that because tomorrow is a Sunday. Anyway I am sure you didn't get the assignment today did you?
Looks like you left it too late to start this project and so rightly you will fail.