Design a circuit and Arduino program that accomplishes the following:
Here is what I have. The code below runs the DC motor and stops when I cover the IR sensor and never changes directions. Please help with code! Maybe I have too much going on here.
//H-Bridge Sensor Motor Control Program
const int EN=9; //Half Bridge 1 Enable
const int MC1=3; //Motor Control 1
const int MC2=2; //Motor Control 2
const int distPin=0; //IR on Analog Pin 0
int val = 0; //for storing the reading from the IR
int velocity = 0; //For storing the desired velocity (from 0-255)
void setup()
{
pinMode(EN, OUTPUT);
pinMode(MC1, OUTPUT);
pinMode(MC2, OUTPUT);
brake(); //Initialize with motor stopped
}
void loop()
{
val = analogRead(distPin);
//go forward
if (val > 563)
{
velocity = map(val, 563, 1023, 0, 255);
forward(velocity);
}
//go backward
else if (val < 462)
{
velocity = map(val, 461, 0, 0, 255);
reverse(velocity);
}
//brake
else
{
brake();
}
}
//Motor goes forward at given rate (from 0-255)
void forward (int rate)
{
digitalWrite(EN, LOW);
digitalWrite(MC1, HIGH);
digitalWrite(MC2, LOW);
analogWrite(EN, rate);
}
//Motor goes backward at given rate (from 0-255)
void reverse (int rate)
{
digitalWrite(EN, LOW);
digitalWrite(MC1, LOW);
digitalWrite(MC2, HIGH);
analogWrite(EN, rate);
}
//Stops motor
void brake ()
{
digitalWrite(EN, LOW);
digitalWrite(MC1, LOW);
digitalWrite(MC2, LOW);
digitalWrite(EN, HIGH);
}
You code is almost okay but need few changes. I edited the code, please try following code. I have simulated this code, and it is working fine. I used pin 6 and 7 to connect H bridge.
//H-Bridge Sensor Motor Control Program
const int EN=9; //Half Bridge 1 Enable
const int MC1=6; //Motor Control 1
const int MC2=7; //Motor Control 2
const int distPin=0; //IR on Analog Pin 0
int val = 0; //for storing the reading from the IR
void setup()
{
pinMode(EN, OUTPUT);
pinMode(MC1, OUTPUT);
pinMode(MC2, OUTPUT);
digitalWrite(EN, HIGH);
Serial.begin(9600);
brake(); //Initialize with motor stopped
}
void loop()
{
val =
analogRead(distPin);
Serial.println(val);
if(val>462){
forward ();
reverse();
}
else {
brake();
}
}
//Motor goes forward at given rate (from 0-255)
void forward ()
{
digitalWrite(MC1, HIGH);
digitalWrite(MC2, LOW);
delay(2000);
}
//Motor goes backward at given rate (from 0-255)
void reverse ()
{
digitalWrite(MC1, LOW);
digitalWrite(MC2, HIGH);
delay(2000);
}
//Stops motor
void brake ()
{
digitalWrite(MC1, LOW);
digitalWrite(MC2, LOW);
}
If you get you motor reversing its direction, don't forget to thumbs up
Design a circuit and Arduino program that accomplishes the following: An IR distance sensor will detect the presence (t...
Please explain to me how the following arduino code control the smart home system design below. #include "ESP8266.h" #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <dht.h> #define DATA_PIN 2 #define SSID "abc" #define PASSWORD "12345678" byte termometerLogo[8] = { B00100, B01010, B01010, B01110, B01110, B11111, B11111, B01110 }; byte humidityLogo[8] = { B00100, B00100, B01010, B01010, B10001, B10001, B10001, B01110, }; SoftwareSerial mySerial(10,11); ESP8266 wifi(mySerial); LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); dht DHT; char buffData[150]; const int...
I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED strips and 3 - ultrasonics. I have them working independently but I need to combine the code so they work together. Here is what I have: Memory Game #define PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s byte sequence[100]; // Storage for the light sequence byte curLen = 0; // Current length of the sequence byte inputCount =...