Offline
Newbie
Karma: 0
Posts: 11
|
 |
« on: February 12, 2013, 10:48:50 pm » |
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
Brattain Member
Karma: 137
Posts: 19053
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: February 13, 2013, 03:39:12 am » |
void Motor::~Motor() { }
void Motor::Motor() /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
Brattain Member
Karma: 313
Posts: 35502
Seattle, WA USA
|
 |
« Reply #2 on: February 13, 2013, 08:17:58 am » |
#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
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #3 on: February 13, 2013, 08:21:52 am » |
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
Brattain Member
Karma: 313
Posts: 35502
Seattle, WA USA
|
 |
« Reply #4 on: February 13, 2013, 08:23:03 am » |
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
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #5 on: February 13, 2013, 08:24:07 am » |
I was supposed to be using Arduino.h but it got out of hand. Should I remove the WProgram include file?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #6 on: February 13, 2013, 08:27:10 am » |
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
Brattain Member
Karma: 137
Posts: 19053
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: February 13, 2013, 08:28:20 am » |
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
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #8 on: February 13, 2013, 08:30:24 am » |
Ok I can do that. Any other gaps in my program?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19053
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: February 13, 2013, 08:35:02 am » |
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
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #10 on: February 13, 2013, 08:43:11 am » |
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; 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 : #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
Brattain Member
Karma: 137
Posts: 19053
I don't think you connected the grounds, Dave.
|
 |
« Reply #11 on: February 13, 2013, 08:53:27 am » |
//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
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #12 on: February 13, 2013, 08:58:00 am » |
A "Begin" method? Im sorry Im a absolute noob can you elaborate? Is that the same as a #define?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19053
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: February 13, 2013, 09:09:47 am » |
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
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #14 on: February 13, 2013, 09:12:37 am » |
So should I add a part to my code that looks like: void Motor::begin() { //Pinmodes here }
|
|
|
|
|
Logged
|
|
|
|
|
|