Go Down

Topic: Newbie looking for program debugging-motion sensor (Read 521 times) previous topic - next topic

acrystalball

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


GrooveFlotilla

A few things here:

If you're debugging the interaction between a rangefinder and a motor, what's with all the seven segment stuff?

Do you still have the LED on pin 13, and if so, what is it doing?

Why are you waiting for a non-existent analogRead?

When you're posting code for people to critique, can you please:
1) post it using the code (#) symbol in the editor's toolbar
2) strip out all extraneous code and whitespace before posting.

What does the output from the serial prints look like?
Are the values sensible?

Why have you declared "echo" and "ultrasoundValue" with global scope?
Code: [Select]
int x = 0;
x = ping();
Why assign zero to something you're going to assign a value to anyway?

Please tell me you're not driving the motor directly from your Arduino.(that could well pull the pin voltage low)

[edit]And why are you cross-posting? Grrrr.[/edit]
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

PaulS

One more question. Why are you not using the same type on both sides of the equal sign here?
Code: [Select]
x = ping();
You declared ping as returning an unsigned long, which you then store in an int.

PS: You need to learn about the else part of the if/else statement. If x is less than 6, it can't possibly be between 6 and 18 or greater then 18.
The art of getting good answers lies in asking good questions.

Go Up