Skip to content
Home » Money box

Money box

In order to control the motor one way and the other, it would be necessary to change the polarity of the motor terminals. This is where the motor driver L293D (or SN754410) helps us out. It is a four-channel motor driver designed to control motors, relays or other inductive electrical devices.

Ultrahelianduri HC-SR04 connecting with Arduino

An ultrasonic sensor is a sensor (sonar) that measures the distance to an obstacle in front using a sound pulse. Namely, the time from the sending of a sound pulse to its reception (echo) is measured. The sound propagation speed of 340m/s must also be taken into account and the result divided by two.

used in test:

  1. arduino uno
  2. arduino plaat
  3. takesti
  4. juhtmed
  5. L293D 
  6. Ultrahelianduri HC-SR04 ühendamine Arduinoga
  7. servo
  8. batarey

SKEEM

Code:

#define ECHO_PIN 8

#define TRIG_PIN 7


int switchPin = 2; // lüliti 1 
int motor1Pin1 = 3; // viik 2 (L293D) 

int motor1Pin2 = 4; // viik 7 (L293D) 

int enablePin = 9; // viik 1(L293D) 

 void setup() { 

 // sisendid 

   
   
 pinMode(ECHO_PIN, INPUT);

 pinMode(TRIG_PIN, OUTPUT);

 Serial.begin(960);
   
 pinMode(switchPin, INPUT);

 //väljundid 

 pinMode(motor1Pin1, OUTPUT); 

 pinMode(motor1Pin2, OUTPUT); 

 pinMode(enablePin, OUTPUT); 

 // aktiveeri mootor1 

 digitalWrite(enablePin, HIGH); 

} 

 void loop() { 

 // kui lüliti on HIGH, siis liiguta mootorit ühes suunas: 
   
 Serial.println(measure()); 

}

int measure()

{

  digitalWrite(TRIG_PIN,HIGH);

  digitalWrite(TRIG_PIN,LOW);

  int distance=pulseIn(ECHO_PIN, HIGH,15000)/50;

  return constrain(distance,1,300);

 if (digitalRead(switchPin) == HIGH) 

{ 

 digitalWrite(motor1Pin1, LOW); // viik 2 (L293D) LOW 

 digitalWrite(motor1Pin2, HIGH); // viik 7 (L293D) HIGH 

 } 

 // kui lüliti on LOW, siis liiguta mootorit teises suunas: 

 else

 { digitalWrite(motor1Pin1, HIGH); // viik 2 (L293D) HIGH 

 digitalWrite(motor1Pin2, LOW); // viik 7 (L293D) LOW 

 } 

} Code language: PHP (php)

Excercise:

komponents:

  1. Arduino Uno
  2. Arendusplaat
  3. takisti
  4. Potintsiomeeter
  5. juhtmed
  6. LCD
  7. rezistor

Code:

#include <LiquidCrystal.h>
 

const int trigPin = 7;
const int echoPin = 8;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
unsigned long objectDetectedTime = 0;
const unsigned long displayDuration = 5000;  
 
byte DOT[8] = {
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111
};
 
void setup() {
  lcd.createChar(0, DOT);  
  lcd.begin(16, 2);  
  pinMode(trigPin, OUTPUT);  
  pinMode(echoPin, INPUT);  
}
 
void loop() {
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  
  long duration = pulseIn(echoPin, HIGH);
 
 
  int distance = duration * 0.034 / 2;
 
 
  if (distance < 200) { 
    if (objectDetectedTime == 0) {
      objectDetectedTime = millis(); 
      fillScreenWithSmiley();
    }
  } else {
    if (objectDetectedTime > 0 && millis() - objectDetectedTime >= displayDuration) {
      objectDetectedTime = 0;  
      clearScreen();
    }
  }
 
  delay(100);
}
 
void fillScreenWithSmiley() {
  lcd.setCursor(0, 0);
  lcd.write(byte(0));
  lcd.write(byte(0));
  lcd.setCursor(14, 0);
  lcd.write(byte(0));
  lcd.write(byte(0));
  lcd.setCursor(0, 1);
  for (int Korda = 0; Korda < 16; Korda++) 
  {
    lcd.write(byte(0));
  }
   
}
 
void clearScreen() {
  lcd.clear();
  lcd.setCursor(2, 1);  
  for (int Korda = 0; Korda < 12; Korda++) 
  {
    lcd.write(byte(0));
  }
}Code language: PHP (php)

Skeem:

video: