0

Try verifying the code to see the errors. This is on a arduino nano with a 328 processor. I put the necessary library in the code. Pastebin for code

 #ifndef Pushbutton_h 
#define Pushbutton_h 


#include <Arduino.h> 


#define PULL_UP_DISABLED    0 
#define PULL_UP_ENABLED     1 


#define DEFAULT_STATE_LOW   0 
#define DEFAULT_STATE_HIGH  1 


/*! \brief The pin used for the button on the 
 * [Zumo Shield for Arduino](http://www.pololu.com/product/2504). 
 * 
 * This does not really belong here in this general pushbutton library and will 
 * probably be removed in the future. */ 
#define ZUMO_BUTTON 12 


class PushbuttonStateMachine 
{ 
public: 
  PushbuttonStateMachine(); 
  bool getSingleDebouncedRisingEdge(bool value); 
private: 
  uint8_t state; 
  uint16_t prevTimeMillis; 
}; 


class PushbuttonBase 
{ 
public: 


  PushbuttonBase(); 


  // wait for button to be pressed, released, or pressed and released 
  void waitForPress(); 
  void waitForRelease(); 
  void waitForButton(); 


  // more complex functions that return true once for each button transition 
  // from released to pressed or pressed to released 
  bool getSingleDebouncedPress(); 
  bool getSingleDebouncedRelease(); 

  // indicates whether button is currently pressed 
  bool isPressed(); 


  // Initializes I/O pin for use as button input. 
  // This gets called automatically so normally you will not need to call this, 
  // but it might be useful if the pin for the button is in the wrong state. 
  virtual void init2() = 0; 


protected: 


  virtual bool _isPressed() = 0; 


private: 


  PushbuttonStateMachine pressState; 
  PushbuttonStateMachine releaseState; 
  bool initialized; 


  // TODO: move all this init stuff down into Pushbutton 
  inline void init() 
  { 
    if (!initialized) 
    { 
      initialized = true; 
      init2(); 
    } 
  } 
}; 


/* \class Pushbutton Pushbutton.h 
 * \brief Interface with pushbuttons on dedicated pins. 
 * 
 */ 
class Pushbutton : public PushbuttonBase 
{ 
public: 


  // constructor; takes arguments specifying whether to enable internal pull-up 
  // and the default state of the pin that the button is connected to 
  Pushbutton(uint8_t pin, uint8_t pullUp = PULL_UP_ENABLED, 
      uint8_t defaultState = DEFAULT_STATE_HIGH); 

  virtual void init2(); 


protected: 


  virtual bool _isPressed(); 


private: 


   uint8_t _pin; 
   bool _pullUp; 
   bool _defaultState; 
 }; 

 #endif 





// PIN Configurations #define SEARCH_SPEED 255 

#define echoPin A0 
#define triggerPin 3 
#define rightBackMotor 4 
#define rightMotorSpeed 10 
#define leftMotorSpeed 9 
#define leftForwardMotor 7  //if a motor goes backward swap the number on the FwdPin and BckPin 
#define rightForwardMotor 8   //if a motor goes backward swap the number on the FwdPin and BckPin 
#define leftBackMotor 11 
#define LED 13 
#define leftSensor A1 
#define rightSensor A2 
#define IRdriver 6 


// Robot Configuration Parameters 
// Speed range is -255 -> 255 
#define MAX_SPEED 255 
#define QTR_Threshold 500 
#define ATTACK_DISTANCE 25 
#define BACKUP_DISTANCE 4000 
#define SEARCH_SPEED 255


// State variables 
bool isBackup = false; 
int backupTimer = 0; 
long distance = 1000; 
Pushbutton button(ZUMO_BUTTON); 


void setSpeed(int speed, int motor); 


void setup() { 
   Serial.begin(9600); 
   Serial.println("Setup."); 


  pinSetup(); 


   Serial.println("Waiting for button"); 
} 


void loop() { 
   digitalWrite(IRdriver, HIGH); 

  if (button.isPressed()) { 
    // if button is pressed, stop and wait for another press to go again 
    button.waitForRelease(); 
    waitForButtonAndCountDown(); 
 } 


  int leftInput = analogRead(leftSensor); 
  int rightInput = analogRead(rightSensor); 


  if ( isBackup ) { 
    backupTimer = backupTimer + 1; 
    setSpeed(-MAX_SPEED, 0); 
    setSpeed(-MAX_SPEED + 20, 1); 
    if (backupTimer >= BACKUP_DISTANCE ) { 
      isBackup = false; 
      backupTimer = 0; 
    } 
  } else { 
    if ( leftInput <= 500 || rightInput <= 500 ) { // WHITE LINE ABORT! 
      //Serial.println("Abort! - "); Serial.println(leftInput); Serial.println(rightInput); 
      isBackup = true; 
      setSpeed(-MAX_SPEED, 0); 
      setSpeed(-MAX_SPEED, 1); 
    } else { 
      distance = getDistance(); 
      if ( distance <= ATTACK_DISTANCE ) { // ATTACK! 
        Serial.println("Attack!"); 
        setSpeed(MAX_SPEED, 0); 
        setSpeed(MAX_SPEED, 1); 
      } else { // search 
        // TODO(erh): implement other searching algorithms 
        Serial.println("Search");  
        setSpeed(SEARCH_SPEED, 0);
        setSpeed(-SEARCH_SPEED, 1); 
      } 
    } 
  } 
} 


void pinSetup() { 
  // motors! 
  pinMode(leftMotorSpeed, OUTPUT); 
  pinMode(rightMotorSpeed, OUTPUT); 
  pinMode(leftForwardMotor, OUTPUT); 
  pinMode(leftBackMotor, OUTPUT); 
  pinMode(rightForwardMotor, OUTPUT); 
  pinMode(rightBackMotor, OUTPUT); 

   // sonar sensor 
  pinMode(LED, HIGH); 
  pinMode(triggerPin, OUTPUT); 
  pinMode(echoPin, INPUT); 


  // qtr light sensor 
  pinMode(leftSensor, INPUT); 
  pinMode(rightSensor, INPUT); 
  pinMode(IRdriver, OUTPUT); 
} 


void setSpeed(int speed, int motor = 0) { 
  int speedPin, forwardPin, backPin; 
  if ( motor == 0 ) { 
    speedPin = leftMotorSpeed; 
    forwardPin = leftForwardMotor; 
    backPin = leftBackMotor; 
  } else if ( motor == 1 ) { 
    speedPin = rightMotorSpeed; 
    forwardPin = rightForwardMotor; 
    backPin = rightBackMotor; 
  } 


  if ( speed == 0 ) { 
    analogWrite(speedPin, 0); 
    digitalWrite(forwardPin, LOW); 
    digitalWrite(backPin, LOW); 
  } else if ( speed > 0 ) { 
    digitalWrite(forwardPin, HIGH); 
    digitalWrite(backPin, LOW); 
    analogWrite(speedPin, speed); 
  } else if ( speed < 0 ) { 
    digitalWrite(forwardPin, LOW); 
    digitalWrite(backPin, HIGH); 
    analogWrite(speedPin, abs(speed)); 
  } 
} 


long getDistance() { 
  long duration, distance; 
  digitalWrite(triggerPin, LOW);  // Added this line 
  delayMicroseconds(2); // Added this line 
  digitalWrite(triggerPin, HIGH); 
  delayMicroseconds(10); // Added this line 
  digitalWrite(triggerPin, LOW); 
  duration = pulseIn(echoPin, HIGH); 
  distance = (duration/2) / 29.1; 
  return distance; 
} 


void waitForButtonAndCountDown() { 
  digitalWrite(LED, HIGH); 
  button.waitForButton(); 
  digitalWrite(LED, LOW); 


  for (int i = 0; i < 3; i++ ) { 
    Serial.println(i); 
    delay(1000); 
  } 


  delay(1000); 
  Serial.println("ATTACK!"); 
  delay(1000); 
}
  • Could you put the error (and code) in this question? – Paul May 3 '16 at 21:05
1

Your code contains only the header declarations (.h file) for those classes. There should also be another file (probably .cpp) that actually implements the code for the classes.

What you pasted looks very similar to the Pushbutton stuff in this project on GitHub.

You can look at their Pushbutton.h and Pushbutton.cpp files to get an idea of what I mean. In fact, you might be able to use those files instead of what you have. I can verify that it does compile, but not whether it works or not.

  • It is the github stuff but I'm new to arduino. Can I take the .cpp file and copy paste the code in because I'm using school computers without much control. – jpb e r May 4 '16 at 11:06
  • Ideally you would download the files (e.g. with the "Raw" link when viewing the files) and use them as a library. But yes I think it should also work if you just copy/paste the contents into your project. Just make sure the .h code is above the .cpp code. I had done that and it compiled okay. – James McManus May 4 '16 at 11:44
  • Could you possibly upload your code because I tried to copy paste but I keep getting previous definition of 'class PushbuttonStateMachine' – jpb e r May 4 '16 at 14:04
  • I don't have any code saved. I just copied your Pastebin code, deleted the old header stuff (everything between #ifndef Pushbutton_h and #endif) and pasted in those other two files instead. – James McManus May 4 '16 at 19:17
0

The error Undefined reference is a linker error, which means that the Arduino ecosystem is missing some of the pieces for the build. If you have indeed provided PushButton.cpp - whether in a library or not - you need to put a #include of Pushbutton.h inside the .ino file. This tells Arduino that you want to include PushButton's code as well as everything else.

If Pushbutton.cpp is inside the same directory as the .ino file then that will "just work". If it's elsewhere, you need to tell Arduino.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.