1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <Metro.h>
//Define the interval
Metro MyInterval = Metro(10);
//Motor 1
int pinAIN1 = 9; //Direction
int pinAIN2 = 8; //Direction
int pinPWMA = 3; //Speed
//Motor 2
int pinBIN1 = 11; //Direction
int pinBIN2 = 12; //Direction
int pinPWMB = 5; //Speed
//Standby
int pinSTDBY = 10;
//Current sensor
const int pinCurrentSensor = A0;
double VoltageValue = 0;
double CurrentValue = 0;
void setup()
{
//Set the PIN Modes
pinMode(pinAIN1, OUTPUT);
pinMode(pinAIN2, OUTPUT);
pinMode(pinBIN1, OUTPUT);
pinMode(pinBIN2, OUTPUT);
pinMode(pinSTDBY, OUTPUT);
pinMode(pinPWMA, OUTPUT);
pinMode(pinPWMB, OUTPUT);
pinMode(pinCurrentSensor, INPUT);
Serial.begin(9600);
}
void loop()
{
VoltageValue = analogRead(pinCurrentSensor)*5.0/1023.0;
//1023 matches with 10-bits available with Arduino UNO.
//This is used to do a conversion
CurrentValue = (VoltageValue - 2.5)/0.185; //0.185 matches with the sensitivity of the sensor
//2,5 matches with the voltage corresponding 0 current
Serial.print("Current = ");
Serial.print(CurrentValue, 3);
Serial.print("\n");
if(MyInterval.check() != 1) //Check if the metro has passed its interval
{
if(CurrentValue < 0.2)
{
digitalWrite(pinAIN1, LOW);
digitalWrite(pinAIN2, HIGH);
digitalWrite(pinBIN1, HIGH);
digitalWrite(pinBIN2, LOW);
digitalWrite(pinSTDBY, HIGH);
analogWrite(pinPWMA,130);
analogWrite(pinPWMB,130);
}
else
analogWrite(pinPWMA,0);
analogWrite(pinPWMB,0);
}
} |
Partager