Dear all,
I have been designing a ultrasonic sensor and use a motor as an proximity alarm.
With the help from the community, I put the codes together so that:
1.) When distance is far enough (>triggerDistance), stops motor, and add a delay;
2) When distance is close enough (<triggerDistance), starts the motor, but stops after a few seconds)
While my codes seems to be working on Arduino Uno, it behaves erratically when I transferred the code to Arduion nano V3.0
Condition 1 have no problems, while condition 2 does not always work.
I guess something wrong with the boolean tripped function or millis function?
#define echoPin 2 // Echo Pin
#define trigPin 3 // Trigger Pin
#define LEDPin 13 // Onboard LED
#define motorPin 19
#define powerPin 4
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int motorState = 0; // motorState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
unsigned long startTime;
long interval = 1000;
boolean lastTrip;
boolean lastTrip2;
//
int maximumRange = 200; // Maximum range needed
int minimumRange = 10; // Minimum range needed
int triggerDistance = 10;
int crashDistance = 5;
//long duration, distance; // Duration used to calculate distance
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
pinMode(powerPin, OUTPUT);
pinMode(motorPin, OUTPUT);
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
long getDistance()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long dist = duration/58.2;
return dist;
}
//////////////////////////////////////////////////////////////////////////////////
void runMotor()
{
if( millis() - startTime < 3000) // Three Second Interval
{
analogWrite(motorPin, 200);
//or digitalWrite(motorPin, HIGH);
}
else
{
analogWrite(motorPin, 0);
//or digitalWrite(motorPin, LOW);
motorState = 0;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void runMotor2()
{
if( millis() - startTime < 3000) // Three Second Interval
{
analogWrite(motorPin, 200);
//or digitalWrite(motorPin, HIGH);
}
else
{
analogWrite(motorPin, 0);
//or digitalWrite(motorPin, LOW);
motorState = 0;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
digitalWrite(powerPin, HIGH);
long distance = getDistance();
if (distance > minimumRange)
{
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
if (distance < minimumRange)
{
Serial.println(distance);
digitalWrite(LEDPin, HIGH);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
boolean tripped = (distance <= triggerDistance ? 1 : 0); // flag if triggered
if (tripped)
{
if (tripped != lastTrip)
{
startTime = millis();
motorState = 1;
}
}
lastTrip = tripped;
if (motorState == 1)
{
runMotor();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
boolean tripped2 = (distance <= crashDistance ? 1 : 0); // flag if triggered
if (tripped2)
{
if (tripped2 != lastTrip2)
{
startTime = millis();
motorState = 2;
}
}
lastTrip = tripped;
if (motorState == 2)
{
runMotor2();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
if (distance > triggerDistance)
{analogWrite(motorPin, 0);
motorState = 0;
delay (500);
}
}
Please have a look on my codes, many thanks in advance!