Pls help me with my school project, idk anything about programming and about hardware

**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:

1. const int trigPin = 9;
2. const int echoPin = 10;

3. float duration, distance;

4. void setup() {
5.   pinMode(trigPin, OUTPUT);
6.   pinMode(echoPin, INPUT);
7.   Serial.begin(9600);
8. }

9. void loop() {
10.   digitalWrite(trigPin, LOW);
11.   delayMicroseconds(2);
12.   digitalWrite(trigPin, HIGH);
13.   delayMicroseconds(10);
14.   digitalWrite(trigPin, LOW);

15.   duration = pulseIn(echoPin, HIGH);
16.   distance = (duration*.0343)/2;
17.   Serial.print("Distance: ");
18.   Serial.println(distance);
19.   delay(100);
20. }

i also bought a motor control driver because a helper on arduino told me that I need it.

:smiley: :smiley:

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.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

Points for the listing. Now, remove all the line numbers; the 1., 2., etc. preceding every line of code and see if it compiles.

1 Like
  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.



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()

//*********************************************************************************

Not possible.

Why do you not want to do your own work?

const int trigPin = 9;
const int echoPin = 10;
float duration, distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * .0343) / 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  if (distance < 15) // cm NOT mm
    digitalWrite(LED_BUILTIN, HIGH);
  else
    digitalWrite(LED_BUILTIN, LOW);
  delay(100);
}

i don't know anything in programming :frowning:

i have 0 knowledge in it, and my teacher wants me to do it as a final project, the due is tomorrow

I was just hoping someone would help me

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.

1 Like

Good. There are better things in life than programming. Find your passion.

4 Likes

Heretic!!!!!!!

So, too, was Galileo for 350 years.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.