Pages: [1]   Go Down
Author Topic: Motor interacting with ultrasonic motion sensor code problems. Please help!  (Read 306 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks Paul!
« Last Edit: August 12, 2012, 06:43:47 pm by rmor2146 » Logged

NYC
Offline Offline
Full Member
***
Karma: 0
Posts: 129
The singularity is near!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are correct in your diagnoses. The trick is that the Arduino supports PWM. This means that you set a pin to a level and you are done. There is no need to worry about the timing within your main loop. 

Read about an Arduino library for this here: http://www.arduino.cc/en/Reference/Servo

And just google "PWM" for a wikipedia article that describes it well.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(distance<500)
{
int motor_speed=255*0; // must be >0 and <= 255
analogWrite( motorpin1, motor_speed);
}
else if(distance>=500)
{
int motor_speed=100; // must be >0 and <= 255
analogWrite( motorpin1, motor_speed);
}
else
{
delay(5000);
}
Crappy indentation aside, under what circumstances will the else block ever be executed?

I don't understand leaving lots of white space between lines and then jamming everything together on the lines you do have. Spaces before and after the = sign, for instance, make the code a lot easier to read.

The Tools + Auto Format menu item will fix the crappy indenting, too.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(distance<500)
{
int motor_speed=255*0; // must be >0 and <= 255
analogWrite( motorpin1, motor_speed);
}
else if(distance>=500)
{
int motor_speed=100; // must be >0 and <= 255
analogWrite( motorpin1, motor_speed);
}
else
{
delay(5000);
}
Crappy indentation aside, under what circumstances will the else block ever be executed?

I don't understand leaving lots of white space between lines and then jamming everything together on the lines you do have. Spaces before and after the = sign, for instance, make the code a lot easier to read.

The Tools + Auto Format menu item will fix the crappy indenting, too.
Sorry bout the crappy indentation.  I tried using the Tools + Auto Format you had mentioned and it didn't fixing the indentation as you mentioned.  Anyways, I meant to input another motor speed function for the else block similarly to the else if function.  I was just wondering if I should rewrite everything that was digital and convert it to analog so that the 0 disappear?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I was just wondering if I should rewrite everything that was digital and convert it to analog so that the 0 disappear?
No idea what that means. What 0?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is an image of what I meant.  

Image seems to be broken, heres a link to the photo via dropbox.  https://www.dropbox.com/s/zu0ckligcpbj1px/serialmonitor.jpg
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Serial.println(distance);
Serial.print(cm);
Serial.print("cm");
Serial.println();
You, too, need to understand the difference between Serial.print() and Serial.println(). You are printing two values on separate lines, and then complaining when you get two lines of output. Stop printing the bogus value.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Paul!  You just pin pointed out my problem!  smiley
Logged

Pages: [1]   Go Up
Jump to: