Howdy,
I am wanting my arduino to control a small vibrating motor and led number display based on data received from a ping ultrasonic sensor... problem is that I can't get any voltage to pin 13 (the pin to control motor relay) when I use the following program:
unsigned long echo = 0;
int ultraSoundSignal = 9; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;
int motorPin = 13; // select the pin for the motort
int ledPinA = 8;
int ledPinB = 2;
int ledPinC = 6;
int ledPinD = 5;
int ledPinE = 4;
int ledPinG = 3;
void setup()
{
Serial.begin(9600);
pinMode(motorPin, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(ledPinD, OUTPUT);
pinMode(ledPinE, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(ultraSoundSignal,OUTPUT);
}
unsigned long ping(){
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
return ultrasoundValue;
}
void loop()
{
int x = 0;
x = ping();
// read the analog input into a variable:
//val = (1024-val1) + 1000;
// print the result:
Serial.println(x);
// wait 10 milliseconds for the analog-to-digital converter
// to settle after the last reading:
delay(10);
if (x < 6) {
digitalWrite(motorPin,LOW);
}
if ((x > 6) && (x < 18) ) {
digitalWrite(motorPin,HIGH);
delay(3000);
digitalWrite(motorPin,LOW);
delay(3000);
lightUpOne();
}
if (x > 18 ) {
digitalWrite(motorPin,HIGH);
delay(6000);
digitalWrite(motorPin,LOW);
delay(3000);
lightUpTwo();
}
}
void lightUpOne(){
digitalWrite(ledPinB, HIGH);
digitalWrite(ledPinC, HIGH);
digitalWrite(ledPinA, LOW); // set the LED on
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinE, LOW);
digitalWrite(ledPinG, LOW);
}
void lightUpTwo(){
digitalWrite(ledPinA, HIGH); // set the LED on
digitalWrite(ledPinB, HIGH);
digitalWrite(ledPinD, HIGH);
digitalWrite(ledPinE, HIGH);
digitalWrite(ledPinG, HIGH);
digitalWrite(ledPinC, LOW);
}
If I upload test programs, such as arduino blink & vary the delay, I get voltage to pin 13, so I am guessing it has something to do with the sensor data in my programming?
I would be grateful if anyone could help me figure this out, it was working before with an IR sensor. Problems started when I switched to an ultrasonic sensor.
Thanks,
Crystal Keesey