Loading...
Pages: [1] 2   Go Down
Author Topic: HELP! I was given a standard Arduino Motor shield, attempted making a library...  (Read 837 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, My names Ryan. I have recently been given an Arduino Uno, and have Ubuntu 10.04.4, and attempted to make a library for the Motor Shield from Arduino. I have failed in my opinion. I have all my code AND error reports. If anyone can help, I would be glad!

Motor.h:
#ifndef Motor_h
#define Motor_h
#include <WProgram.h>
#include <Arduino.h>

class Motor
{
public:
void ~Motor();
void Motor(void);
void forward(int speed);
void backward(int speed);
void right(int speed);
void left(int speed);
void brake();



};
#endif

Motor.cpp:
#include <Motor.h>

#include <Arduino.h>

int speed;
const int dirB = 13;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int pwmA = 3;

void Motor::~Motor()
{
}

void Motor::Motor()
{

int speed;
const int dirB = 13;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int pwmA = 3;


  pinMode(dirB, OUTPUT);
  pinMode(dirA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  pinMode(pwmA, OUTPUT);
}

void Motor::brake()
{
digitalWrite(brakeA, HIGH);
digitalWrite(brakeB, HIGH);
}


void Motor::forward(int speed)
{
  digitalWrite(brakeA, HIGH);
  digitalWrite(brakeB, HIGH);
  delay(10);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
 
  digitalWrite(dirA, HIGH); //set A forward
  digitalWrite(dirB, HIGH); // Set B forward
  analogWrite(pwmA, speed);
  analogWrite(pwmB, speed);
}

void Motor::right(int speed)
{
  digitalWrite(brakeA, HIGH);
  digitalWrite(brakeB, HIGH);
  delay(10);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
 
  digitalWrite(dirA, LOW);
  digitalWrite(dirB, HIGH);
  analogWrite(pwmA, speed);
  analogWrite(pwmB, speed);
}

void Motor::left(int speed)
{
  digitalWrite(brakeA, HIGH);
  digitalWrite(brakeB, HIGH);
  delay(10);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
 
  digitalWrite(dirA, HIGH);
  digitalWrite(dirB, LOW);
  analogWrite(pwmA, speed);
  analogWrite(pwmB, speed);
}

void Motor::backward(int speed);
{
  digitalWrite(brakeA, HIGH);
  digitalWrite(brakeB, HIGH);
  delay(10);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
 
  digitalWrite(dirA, LOW);
  digitalWrite(dirB, LOW);
  analogWrite(pwmA, speed);
  analogWrite(pwmB, speed);
}

code to run motors:
#include <Motor.h>
Motor Motor;



void setup()
{

}

void loop()
{
  Motor.forward(255);
}

Error report:
In file included from Tester.ino:1:
/home/chaos/Desktop/arduino-1.0.3/libraries/Motor/Motor.h:9: error: return type specification for destructor invalid
/home/chaos/Desktop/arduino-1.0.3/libraries/Motor/Motor.h:10: error: return type specification for constructor invalid

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 137
Posts: 19053
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void Motor::~Motor()
{
}

void Motor::Motor()
Quote
/home/chaos/Desktop/arduino-1.0.3/libraries/Motor/Motor.h:9: error: return type specification for destructor invalid
/home/chaos/Desktop/arduino-1.0.3/libraries/Motor/Motor.h:10: error: return type specification for constructor invalid
Some compiler error messages can be really obscure.
Not here though.


Hint for the future: don't put "pinMode" in a constructor for the Arduino.

Please use code tags when posting code.
« Last Edit: February 13, 2013, 03:41:25 am by AWOL » Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

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

Code:
#include <WProgram.h>
#include <Arduino.h>
For any version of the IDE, only one of these exists. Why are you trying to include both, without the proper #ifdef directives?
Logged

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

Thank you! So if I just move the pinMode outside of Motor::Motor() the compiler should stop giving me errors? I'm a absolute noob at this business; I am a veteran C coder and I thought I was up to stuff. Thanks for clearing it up!
Logged

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

Quote
So if I just move the pinMode outside of Motor::Motor() the compiler should stop giving me errors?
No, but that is necessary.

Constructors and destructors do not have types, not even void.
Logged

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

I was supposed to be using Arduino.h but it got out of hand. Should I remove the WProgram include file?
Logged

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

So as for can you show me about what it should look like? I just started typing this yesterday, and have a deadline coming soon. Thanks
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 137
Posts: 19053
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Should I remove the WProgram include file?
If you're using IDE 1.0 or later, yes.
Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

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

Ok I can do that. Any other gaps in my program?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 137
Posts: 19053
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Apart from the limited scope of the constant declarations in the constructor?

Post your corrected code.
Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

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

Motor.cpp :
Code:
#include <Motor.h>

#include <Arduino.h>

int speed;
const int dirB = 13;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int pwmA = 3;
 pinMode(dirB, OUTPUT);
  pinMode(dirA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  pinMode(pwmA, OUTPUT);

void Motor::~Motor()
{
}

void Motor::Motor()
{

int speed;
const int dirB = 13;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int pwmA = 3; //Should I leave these guys? Or is that bad?


  
}

void Motor::brake()
{
digitalWrite(brakeA, HIGH);
digitalWrite(brakeB, HIGH);
}


void Motor::forward(int speed)
{
  digitalWrite(brakeA, HIGH);
  digitalWrite(brakeB, HIGH);
  delay(10);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
  
  digitalWrite(dirA, HIGH); //set A forward
  digitalWrite(dirB, HIGH); // Set B forward
  analogWrite(pwmA, speed);
  analogWrite(pwmB, speed);
}

void Motor::right(int speed)
{
  digitalWrite(brakeA, HIGH);
  digitalWrite(brakeB, HIGH);
  delay(10);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
  
  digitalWrite(dirA, LOW);
  digitalWrite(dirB, HIGH);
  analogWrite(pwmA, speed);
  analogWrite(pwmB, speed);
}

void Motor::left(int speed)
{
  digitalWrite(brakeA, HIGH);
  digitalWrite(brakeB, HIGH);
  delay(10);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
  
  digitalWrite(dirA, HIGH);
  digitalWrite(dirB, LOW);
  analogWrite(pwmA, speed);
  analogWrite(pwmB, speed);
}

void Motor::backward(int speed);
{
  digitalWrite(brakeA, HIGH);
  digitalWrite(brakeB, HIGH);
  delay(10);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
  
  digitalWrite(dirA, LOW);
  digitalWrite(dirB, LOW);
  analogWrite(pwmA, speed);
  analogWrite(pwmB, speed);
}

Motor.h :
Code:
#ifndef Motor_h
#define Motor_h
#include <Arduino.h>

class Motor
{
public:
void ~Motor();
void Motor();
void forward(int speed);
void backward(int speed);
void right(int speed);
void left(int speed);
void brake();



};
#endif
Hope i got that right!

Moderator edit: CODE TAGS
« Last Edit: February 13, 2013, 08:54:56 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 137
Posts: 19053
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
//Should I leave these guys? Or is that bad?
No, not bad, just pointless. Think about the scope of those declarations.

You've taken out the "pinMode"s completely, but they're necessary.
In Arduinoland, it's more usual to put them in a "begin" method.

Have a look at some of the provided libraries for hints.
Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

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

A "Begin" method? Im sorry Im a absolute noob can you elaborate? Is that the same as a #define?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 137
Posts: 19053
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Classes like the "Serial" class have a "begin" method, because constructors are called before the hardware timers for the Arduino are setup, so setting the baud rate in a constructor would be pointless.
Same goes for "pinMode", so your constructor is either empty, or copies its parameters to its own private variables, but does nothing with them.
The "begin" method, then takes the private variables, and sets the "pinMode"s, or you supply the "begin" method with the pin numbers.

In your case, you're assuming a set of fixed pin numbers, so the "begin" method" would use those.
Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

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

So should I add a part to my code that looks like:
void Motor::begin()
{
//Pinmodes here
}
Logged

Pages: [1] 2   Go Up
Print
 
Jump to: