Prob writing a code for HB motor and accelerometer

Hi,
I am writing a code to control 2 servo motors with an accelerometer. One motor for each axis.

First I tried this program to make sure my servo is working.

#include
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
myservo.write(180);
delay(15);
}

As you you the servo would work on pin 9.

I tried editing the same to make 2 servos works with the code below. didn’t yield any result. I am not into coding and I am having a real tough time fixing this issue.

I even used the memsic 2125 debug serial statement and tried making changes to make the servos work but hasn’t been helpful at all.

If anyone has any suggestions or program of how to make control a HS325Hb Servo motors with a accelerometer will be really helpful.

I need to submit this as a project and I’m looking forward for some help regarding the same.

If any one has programmed it earlier PLEASE let me know, any help regarding the matter would be great. Sorry for all the trouble.

Thank you all

That sketch isn't what you have because it simply won't compile. Why don't you post what you've got?

Even if it did compile, the servo would just sit at a single position.

If anyone has any suggestions or program of how to make control a HS325Hb Servo motors with a accelerometer will be really helpful

Define "control".

int servoPin = 9; // Control pin for servo motor int minPulse = 500; // Minimum servo position int maxPulse = 3500; // Maximum servo position int pulse = 0; // Amount to pulse the servo

long lastPulse = 0; // the time in milliseconds of the last pulse int refreshTime = 1000; // the time needed in between pulses

int xPinValue = 0; // the value returned from the analog sensor int xPin = 2; // X output of the accelerometer

void setup() { pinMode(xPin, INPUT); pinMode(servoPin, OUTPUT); // Set servo pin as an output pin pulse = minPulse; // Set the motor position value to the minimum Serial.begin(9600); }

void loop() { int pulseX; int accelerationX; pulseX = pulseIn(xPin,HIGH); accelerationX = ((pulseX / 10) - 500) * 8; Serial.print(accelerationX); Serial.print("\t"); Serial.println(); delay(1000); xPinValue = digitalRead(xPin); // read the analog input pulse = map(xPinValue,0,4000,minPulse,maxPulse); // convert the analog value // to a range between minPulse // and maxPulse.

// pulse the servo again if rhe refresh time (20 ms) have passed: if (millis() - lastPulse >= refreshTime) { digitalWrite(servoPin, HIGH); // Turn the motor on delayMicroseconds(pulse); // Length of the pulse sets the motor position digitalWrite(servoPin, LOW); // Turn the motor off lastPulse = millis(); // save the time of the last pulse } }

The actual program...with serial print on screen

hi AWOL,

The main problem here is the servo is not connecting with the a.meter
The connection is missing somewhere but the program is being compiled and im able to upload it :’(

xPinValue = digitalRead(xPin);      // read the analog input
 pulse = map(xPinValue,0,4000,minPulse,maxPulse);

xPinValue will be in the range 0 to 1023. You are mapping this using a FROM range of 0 to 4000. Can you explain why?

There is a very nice servo library that you are not using. Can you explain why?

None of this stuff is used:

  int pulseX;
  int accelerationX;
  pulseX = pulseIn(xPin,HIGH);
  accelerationX = ((pulseX / 10) - 500) * 8;
  Serial.print(accelerationX);
  Serial.print("\t");
  Serial.println();

Can you explain what is supposed to do, and why it is there?

Is refreshing the servo once a second often enough?

What kind of servo is it?

Ignoring the accelerometer, for the moment, can you make the servo(s) moving using a simple loop to pulse the servo?

Hi Paul,

I was trying to understand and use the "pulse method" which was given in the tutorial. i was trying to understand that. and I didn't know it was only between 0 to 1023, I was thinking 4000 because i thought servo controlling differs from potentionmeter to a.meter.

I was playing with diffrent values of refreshing time and I posted the one with one sec. I know it is too large and it must be preferably around 10milli.

The servo is a HS 3 2 5 h b

the simple loop to pulse the servo I believe is defined in the example, which I have tried and works. the one in which we can rotate it to 180 and make it stop or conitnuiously make it rotate frm 0-180 or 180-0.

I feel the main problem with me understanding how to code btw an analog and digital. It is confusing me a lot with the understanding the coding as I'm not of that major and am trying to learn for experience.

thank you. Please let me know how should I approach, it will be of great help. thank you again

OK, so the map function call should be changed so that the FROM range is 0 to 1023.

You might have missed the question about why you are not using the servo library. People spend a lot of time writing libraries so that people who don't understand programming can simply use a servo.

A few calls, like servo.attach and servo.write will have your servo moving like you want far faster than you trying to replicate the servo library.

So, why are you not using the servo library?

Presuming that the data you are getting from the accelerometer correlates at all to movement of the accelerometer, you could have this project done today, if you use the servo library. There are even good examples provided with the library.

hi Paul,

thank you

Will look into it and try the program. will get back if i have any doubts or concerns .

Hi paul, the reason for me not to use the servo library is because I thought doing the longer way will help me understand the concept better and maybe if i made some changes in the program with analog control to digital control will make the program run the way i want it.

As I have said before coding has never been my forte and this is the first time when i'm putting it to actual use. I guess im still learning the methods, maybe as a learner I know I want to go in a particular direction but too many things to understand are making me divert into a different direction.

But i understand that defining the servo has nothing to do with the other controlling. I have one more question. as you see in my programming part i tried to generate a serial debug print statement. so without using the servo attach I see both of them working seperately. I presume that i need to attach or use the "servo attach" in void setup. but my question is when i try to replicate in general if i achieve what i want just on the breadboard without the duemilanue board and just the atmega chip would that burn out my accelerometer? If so is ther any solution to fix the problem or should i completely replicate the milanue on the breadboard?

thank you

As I have said before coding has never been my forte and this is the first time when i’m putting it to actual use. I guess im still learning the methods, maybe as a learner I know I want to go in a particular direction but too many things to understand are making me divert into a different direction.

So the way to approach this is to find libraries that do what you want to do. Learn to use the libraries. Then, one at a time, replace the functionality of the library with your own code. As you develop skills, you can, for a time, not depend on libraries. After a while, though, you’ll see the advantage of using libraries where they provide functionality that exceeds your capabilities or is outside of your area of interest.

The servo library is a good example. If you want to make a controller for an art project, the coding to make the servos move is not something you want to spend time developing. If, instead, you want to make a line following robot, the line following part of the project is the tricky part. Making the left wheel(s) turn, or the right wheel(s) is not the challenging part. Making them turn in the right direction, at the right time, at the right speed, is.

I presume that i need to attach or use the “servo attach” in void setup.

Setup is the name of the function. Void is it’s return type. Newbies refer to the function as void setup. Experienced programmers know better. But, yes, that is where to attach the servo.

You would be far better off, at this stage, connecting stuff directly to the Arduino than in trying to build your own Arduino on a breadboard.

Use a developed, tested, functioning product while getting the software working. Then, with functioning software, you can recreate the hardware on your breadboard or handmade or designed/professionally manufactured PCB.

Take small steps to start with. The frustration level will be much lower, and it will be easier to stick with it, if you are not facing non-functioning hardware AND software at the same time.

 #include <Servo.h>      // include the servo library

 Servo servoMotor;       // creates an instance of the servo object to control a servo

 int xPinValue = 0;  // the value returned from the digital sensor
 int xPin = 2;            // X output of the accelerometer

 int servoPin = 9;       // Control pin for servo motor, may only be pin 9 or 10

 void setup() { 
   pinMode(xPin, INPUT);
   pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
   servoMotor.attach(servoPin);  // attaches the servo on pin 2 to the servo object
   servoMotor.write(90);
   Serial.begin(9600);
 } 

 void loop() 
 { 
    int pulseX;
    int accelerationX;
    pulseX = pulseIn(xPin,HIGH);
    accelerationX = ((pulseX / 10) - 500) * 8;
    xPinValue = digitalRead(accelerationX);                 // read the analog input (value between 0 and 1023)
    xPinValue = map(xPinValue, 0, 1023, 0, 179);     // map the analog value (0 - 1023) to the angle of the servo (0 - 179)
    servoMotor.write(xPinValue);    // write the new mapped analog value to set the position of the servo
    delay(15);                                           // waits for the servo to get there 
 }

The code is still not yielding any results. This is the code i tried redoing using servo library. I’m not sure where it is going wrong, no errors are detected. but i am not sure where i missed the required connection.

ANY HELP.

Thank YOU :-/

xPinValue = digitalRead(accelerationX);                 // read the analog input (value between 0 and 1023)

Can't see that working. Maybe you ought to check your wiring and your analogue pins.

Hi, the previous code had some minor changes even then there was no changes in servo reaction.

 #include <Servo.h>      // include the servo library

 Servo servoMotor;       // creates an instance of the servo object to control a servo

 int xPinValue = 0;  // the value returned from the digital sensor
 int xPin = 2;            // X output of the accelerometer

 int servoPin = 9;       // Control pin for servo motor, may only be pin 9 or 10

 void setup() {
   pinMode(xPin, INPUT);
   pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
   servoMotor.attach(servoPin);  // attaches the servo on pin 2 to the servo object
   servoMotor.write(90);
   Serial.begin(9600);
 }

 void loop()
 {
    int pulseX;
    int accelerationX;
    pulseX = pulseIn(xPin,HIGH);
    accelerationX = ((pulseX / 10) - 500) * 8;
    xPinValue = digitalRead(accelerationX);                 // read the digital input 
    xPinValue = map(xPinValue, 0, 1023, 0, 179);     // map the digital value (0 - 1023) to the angle of the servo (0 - 179)
    servoMotor.write(xPinValue);    // write the new mapped digital value to set the position of the servo
    delay(15);                                           // waits for the servo to get there
 }

ANY HELP WHERE I need to change in order to make it run properly…

thank you

xPinValue = digitalRead(accelerationX);                 // read the digital input
    xPinValue = map(xPinValue, 0, 1023, 0, 179);

Right there.

Read it [u]really[/u] carefully

HI Groove

xPinValue = digitalRead(accelerationX);                 // read the digital input
    xPinValue = map(accelerationX, 0, 1023, 0, 179);

Even Now the setup is not yielding any results

I have a question, are my both components attached at this point through my coding. I am new to this, so I am getting confused :-/

ANy HELP

Thanks

OK, you read it really carefully?

There are two very well documented function calls in that code segment.
Read, and re-read their specifications, particularly the first.

And then ask “What’s the point of mapping a range of 0…1 to a range 0…179?”

{
    int pulseX;
    int accelerationX;
    pulseX = pulseIn(xPin,HIGH);
    accelerationX = ((pulseX / 10) - 500) * 8;
    xPinValue = digitalRead(accelerationX);                 
    [b]xPinValue = map(xPinValue, 0, 1023, 0, 179);[/b]     
    servoMotor.write(90);   
    delay(15);                                          
 }

It compiles but doesnt get attached with both components again. I know there is something wrong in the bold line. I dont know where i am running wrong exactly.

Need help ... :'(

There is nothing whatsoever wrong with the highlighted line.

The problem, as I pointed out yesterday, is with the line before it - __digital__Read.

Did you, as Groove suggested, read the definition?
http://arduino.cc/en/Reference/DigitalRead

Hi AWOL, as suggested if i change the "accelerationX" to the pin of a.meter, then who does the acceleration get defined. as given in the notes. How should I be defining the acceleration code, as you guys know Im trying to make the servo react to the acceleration of the a.meter.

Could you actual help me understand the the code version better, so that i get a better understanding. :(

thank you

digitalRead returns HIGH (1) or LOW (0). No other values, no decimal fractions - a simple digital, binary value. It does not, please note, return a value between 0 and 1023.

I can't remember what the value of "accelerationX" is, but I'm hoping that it is an integer between zero and five, and that you have the accelerometer X output connected to the corresponding ANALOGUE pin. [edit]Ah! OK although the device is an analogue device, you have to use "pulseIn" to measure the acceleration. So, it does need to be connected to a digital pin (or an analogue pin using the range 14...19), but "digitalRead" is still quite useless. Apologies. The "map" needs to be rexamined when you've worked out what number range to map from. Looks like you've already got a pulsin - why did you overwrite the result? [/edit]