Controlling a DC motor with RC signal Motorshield V2

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
carlosap
 
Posts: 45
Joined: Tue Feb 17, 2015 5:13 pm

Controlling a DC motor with RC signal Motorshield V2

Post by carlosap »

I got my receiver hocked up to my shield an use a pulsein() to get the pwm signal. I grab and do some math to translate into motor speed 0-255. below is the code. It doesnt work, the motor will just skip. here is the code, I am a noob so there is probably a better way to do this

Code: Select all

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

int ElevChpin = 4;


Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *LeftMotor = AFMS.getMotor(3);

unsigned long ThrotlefromRC;
long MotorSpeed;


void setup()
{
	pinMode(ElevChpin, INPUT);


	AFMS.begin();
}

void loop()
{
	ThrotlefromRC = pulseIn(ElevChpin, HIGH, 25000);

	if (1498 <= ThrotlefromRC &&  ThrotlefromRC <= 1510)
	{
		LeftMotor->run(RELEASE);
		LeftMotor->setSpeed(0);
	}
	if (ThrotlefromRC > 1510)
	{
		MotorSpeed = ((ThrotlefromRC*0.13) - 195)*4.8;
		LeftMotor->run(FORWARD);
		LeftMotor->setSpeed(MotorSpeed);
	}
	if (ThrotlefromRC < 1498)
	{
		MotorSpeed = ((ThrotlefromRC*0.13) - 195)*-4.8;
		LeftMotor->run(BACKWARD);
		LeftMotor->setSpeed(MotorSpeed);
	}
}
Last edited by Franklin97355 on Thu Feb 26, 2015 5:04 pm, edited 1 time in total.
Reason: Added missing [code] tags

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: Controlling a DC motor with RC signal Motorshield V2

Post by Franklin97355 »

One thing I see is you are trying to do float math without any floats.

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling a DC motor with RC signal Motorshield V2

Post by adafruit_support_bill »

It doesnt work, the motor will just skip.
What kind of motor? What do you mean by 'skip'? That is not a term typically used to describe the performance of a DC motor.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”