Coder Social home page Coder Social logo

2022-capteur-au-choix-gitgudshu's Introduction

Exercice avec deux capteurs aux choix

1. Le projet

Un stepper motor tourne dans le sens horaire de manière continue et une led rouge est allumée, lorsque l'on place quelque chose devant le capteur ultrasonique, la led verte s'allume et le stepper motor tourne dans le sens anti-horaire jusqu'à une certaine quantité déterminée de step.

2. Composants

  • Arduino UNO
  • 2 LED (generic)
  • 2 Resistor 220 ohm
  • Jumper wires
  • Pile de 5V
  • Stepper Motor 28BYJ-48
  • ULN2003 Driver Board

3. Code

Il faudra include la librairie Stepper.h

#include <Stepper.h>

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

int redpin = 4;
int greenpin = 5;

// Defines the number of steps per rotation
const int stepsPerRevolution = 2038;

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

// Creates an instance of stepper class
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
	pinMode(greenpin, OUTPUT);
  pinMode(redpin, OUTPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
}

void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)

	myStepper.setSpeed(10);
  
  if(distance < 10){
    digitalWrite(greenpin, HIGH);
    digitalWrite(redpin, LOW);
    myStepper.step(stepsPerRevolution);
  }else{
    digitalWrite(greenpin, LOW);
    digitalWrite(redpin, HIGH);
    myStepper.step(-stepsPerRevolution);
  }
}

4. Diagrammes fritzing

Les 2 nouveaux composants ont été séparés pour plus de clareté, seuls l'arduino UNO et le circuit pour les LED sont en communs.

Stepper motor

alt text

Capteur ultrasonique

alt text

5. Sources

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.