DC Motor help (two wires)

Hi Hootie81
yep got it thank you, TBH i had to ctrl+F the ‘:’ to find it haha.

Now just need to figure how to work it online and on demand rather than constantly on

Dave

did it work to control the motor in both directions?

Hi mate
Yep it sure did, as I say just need to figure out how to control it on the web.
Both full revolutions and minor ones too

Dave

I’m guessing what i need to figure out is how to translate this into the above

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position

void setup()
{
  myservo.attach(D0);  // attaches the servo on the A0 pin to the servo object
  Spark.function("setpos", setPosition);
  Spark.variable("getpos", &pos, INT);
}

void loop()
{
}

int setPosition(String posValue) {
    pos = posValue.toInt();
    myservo.write(pos);
    return 0;
}

Any help anyone?

Sorry

@dspalton, what do you mean by translate this into the above?

Trying to figure out how to get it to be controlled over the web, for opening and closing

Dave

@dspalton, you need to look at Particle.function()

There is also an example using PhoBot and Particle.function() in the example here: https://github.com/simonmonk/PhoBot/blob/master/firmware/examples/WebControl.cpp

Just wanted to add a friendly reminder that if you let the community know what you've tried that we'll be able to help you better. We all had so start somewhere and want to help you, instead of doing your project for you :slight_smile:

2 Likes

Since you already are using Particle.function() I'd rather think that you want to translate

  ...
  pos = posValue.toInt(); 
  myservo.write(pos);
  ...

into someting like p.setMotors("M4-F-100");, or not?

And for this you might want to look into C standard functions like sprintf().

2 Likes

I just don’t understand how to get this into a readable format for running over the web.

Sorry i am totally new at this etc…
Appreciate any help / advice or even commented code to show me how its working

Dave

What exactly do you want to get into which readable format and where would you intend to display/present it?

What I’m looking to do is use a webpage similar to the example to control the motor when called.
Just now the code we have above and working just turns the motor on, stops, goes backwards etc.
So it’s controlling it online. Using a browser.
As you say above it is prob like

p.setMotors(“M4-F-100”);

But it’s getting the variables etc that’s confusing me.

Sorry, but I do thank you for the help and advice

Dave

OK, if you want something like that control code, you can make up your string like this

char motorCommand[12];
uint motorNr;
int speed;

...

  if (speed==0)
    sprintf(motorCommand, "M%u-S", motorNr);
  else
    sprintf(motorCommand, "M%u-%s-%d", motorNr, (speed > 0 ? "F" : "B"), abs(speed));

  p.setMotors(motorCommand);

Or you just send a prefabricated command into a Particle.function(). The way how’d do that, depends on your remote code language.

Thank you for that

I’ll,check the function link as well there.

What does each step there do and signify as such.

Dave

Obviously you haven't done this then

So just a quick primer, but you should get yourself acquainted with C if you want to get somewhere on your own some time.

First I just set up some variables to be used for the rest.

Then I check if speed is zero to create the stop command "Mx-S" where x stands for the motor number. And this is what sprintf() does, it takes the format string (second parameter) and substitutes %u for an unsigned number (hence %u) given as the first parameter after the format string and stores the resulting string into the variable passed in as first parameter.

If speed is not zero, there are three values to substitute in the format string: motorNr (as above) the direction F or B where F will be used if speed is positive or B otherwise, and finally the absolute (positive only) value of speed.

So given motorNr = 1 and speed = +20 you'll end up with "M1-F-20" or for speed = -30 it'll be "M1-B-30".

That’s brilliant explaining thank you.
I’ll have a play during the day (it’s 0209 here in the UK) and let you know how I get one

I am actually just about to start part of an open university degree in robotics and Linux so it will hopefully allow me more info.

Thank you

Yup! It’s 3:17 here in Austria :wink:

Working or just awake?
I’m working :flushed:

Working too :tired_face: