Hi all,
I am trying to make an interactive installation where motor powered movement is activated as viewers get closer to the exhibit. I will endeavor to add some limit switches later. the movement does not need to be precise.
I have put together some code (several attempts - not one of my strengths) and get a reading from the serial monitor, but only a barely audible response from the motor.
Any advice would be much appreciated.
include <NHCSR04.h>
int maxCentimeters = 500; //Default is 100cm. This is to prevent too long ping
int baudrate = 9600;
int triggerPin = 13;
int echoPin = 12;
SR04 sensor(triggerPin, echoPin, maxCentimeters);
double cm;
//Initialize other motor control pins;
int EnableMotorUp = 3;
int EnableMotorDown = 4;
int PwmSignalUp = 5;
int PwmSignalDown = 6;
int oldvalue = 0; //create variable for comparing input signalfrom sensor
void setup()
{
Serial.begin(baudrate); // We initialize serial connection so that we could print values from sensor.
// Setup the outputs.
pinMode(EnableMotorUp, OUTPUT);
digitalWrite(EnableMotorUp, HIGH);
pinMode(EnableMotorDown, OUTPUT);
digitalWrite(EnableMotorDown, HIGH);
pinMode(PwmSignalUp, OUTPUT);
pinMode(PwmSignalDown, OUTPUT);
}
void loop()
{
cm = sensor.centimeters();
Serial.print("cm: ");
Serial.println(cm);
int val = sensor.centimeters(); // Read value from sensor
//If the reading from the sensor is greater than the previous value
if (val < oldvalue)
{
digitalWrite(PwmSignalUp, HIGH);
digitalWrite(PwmSignalDown, LOW);
}
else if (val > oldvalue)
{
digitalWrite(PwmSignalDown, HIGH);
digitalWrite(PwmSignalUp, LOW);
}
else (val == oldvalue);//STOP MOTOR
{
digitalWrite(PwmSignalDown, LOW);
digitalWrite(PwmSignalUp, LOW);
}
oldvalue = val;
}