Go Down

Topic: was not declared in this scope (Read 278 times) previous topic - next topic

sanderpander

Jan 17, 2019, 11:55 pm Last Edit: Jan 19, 2019, 03:08 pm by sanderpander
Hi, I get the following error when I try to verify my code:


Code: [Select]
In function 'void moveServo(Servo, int, int, int)':

Puzzle_Box:174:16: error: 'servoPin' was not declared in this scope

servo.attach(servoPin, minVal, maxVal);


This is the code I try to verify:

Code: [Select]
#include <Servo.h>
#include "PegBoard.h"
#include "Connection.h"

#define INPUT_PULLUP 0x2
#define OPEN LOW
#define CLOSED HIGH

// Initialize the PegBoard
const byte pegCount = 10;
// Specify the pins for the peg board connectors
// NOTE: If using pins 0 and 1, we can't do serial communication at the same time.
const byte pegPins[pegCount] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
PegBoard pegBoard = PegBoard(pegCount, pegPins);

// Initialize the LEDs
const byte ledCount = 6;
// Specify the pins for the pegboard LEDs
const byte ledPins[ledCount] = { A0, A1, A2, A4, A3, A5 };

// Initialize the success and failure sounds
//const byte successSoundPin = 13;
//const byte failureSoundPin = 12;

// Set up our combinations
const Connection pegCombos[ledCount] = {
    // Here are the expected combinations, and their order
    // NOTE: The nature of this pegboard makes it impossible
    //       to require a connection between the same digits
    //       (e.g. You can never wire up 2--2)
    // Change these (obviously) to the combinations you would like to use.
    Connection(0,1),
    Connection(0,2),
    Connection(0,3),
    Connection(0,4),
    Connection(0,5),
    Connection(0,6)
    // That's the kind of thing an idiot would have on their luggage!
    // https://www.youtube.com/watch?v=_JNGI1dI-e8
};

Servo rightDrawerServo;
Servo keyDrawerServo;
// Specify the pins for the two servo control lines
const byte rightDrawerServoPin = 10;
const byte keyDrawerServoPin = 11;
// Min/max PWM values - will likely have to fiddle with these per servo
const int rightMin = 1000;
const int rightMax = 2200;
const int keyMin = 800;
const int keyMax = 2125;

// How many combinations have we solved thusfar
byte progress = 0;


void setup() {
  // Enable serial connection
  //Serial.begin(9600);

  // Set up the peg board pin modes
  for (byte i=0; i<pegCount; i++) {
    pinMode(pegPins[i],INPUT_PULLUP);
  }

  // Set all LED pins as OUTPUT
  for (byte j=0; j<ledCount; j++) {
    pinMode(ledPins[j],OUTPUT);
  }

  // Sound system
  //pinMode(successSoundPin, OUTPUT);
  //pinMode(failureSoundPin, OUTPUT);

  // Play a success sound
  //digitalWrite(successSoundPin,HIGH);
  //delay(100);
 
  // On power up, we want this drawer to open
  openRightDrawer();
}

void loop() {
  // Check if there are connections
  if (pegBoard.hasNewConnection()) {
    Connection conn = pegBoard.getConnection();
    conn.print();
    if (conn.isConnected()) {
      // New connection matches expected combination
      if (conn == pegCombos[progress]) {
        Serial.print("  Good connection! Lock ");
        Serial.print(progress+1);
        Serial.println(" disabled.");
 
        // Light up an LED
        Serial.print("LED ");
        Serial.print(progress+1);
        Serial.println(" on.");
        digitalWrite(ledPins[progress],HIGH);
 
        // Move forward in the combination
        progress++;
 
        // We've connected all the peg pairs
        if (progress >= ledCount) {
          Serial.println("Access granted.");
         
          // Play a success sound
          //digitalWrite(successSoundPin,HIGH);
          //delay(100);
          //digitalWrite(successSoundPin,LOW);

          // Flash all LEDs a few times
          for (int j=0; j<=5; j++) {
            for (int i=0; i<=ledCount; i++) {
              digitalWrite(ledPins[i],LOW);
            }
            delay(250);
            for (int i=0; i<=ledCount; i++) {
              digitalWrite(ledPins[i],HIGH);
            }
            delay(250);
          }
         
          // Open the key drawer
          openKeyDrawer();

          // Stop failure sound
          //digitalWrite(failureSoundPin,LOW);
        }
      }
      // Bad connection - reset progress
      else {
        Serial.println("  Bad connection. All locks resetting.");
           
        // Turn off all LEDs in reverse order
        for (int i=progress-1; i>=0; i--) {
          // Play failure sound
          //digitalWrite(failureSoundPin,HIGH);
           
          // Turn an LED off
          Serial.print("LED ");
          Serial.print(i+1);
          Serial.println(" off.");
          digitalWrite(ledPins[i],LOW);
          delay(500);

          // Stop failure sound
          //digitalWrite(failureSoundPin,LOW);
        }
       
        // If the box had been solved, and an incorrect connection is made,
        // move the servos back to their starting positions.
        if (progress >= ledCount) {
          Serial.println("Resetting servos...");
          // Close the drawers
          resetServos();
        }
       
        // Reset progress to 0
        progress = 0;
      }
    }
  } 
}

// Moves the specified servo to the requested degree position.
// Requires the specification of min/max values.
// Attaches to the servo, moves it, and detaches. This means that
// the servo won't hold its position, but also won't jitter
// or continue to draw battery. This was sufficient for this use.
void moveServo(Servo servo, int position, int minVal, int maxVal) {
  servo.write(position);
  servo.attach(servoPin, minVal, maxVal);
  delay(500);
  servo.detach();
}

// Causes the servo to move such that the right drawer opens
void openRightDrawer(){
  Serial.println("Opening right drawer");
  moveServo(rightDrawerServo, 180, rightMin, rightMax);
}
// Causes the servo to move such that the right drawer closes
void closeRightDrawer(){
  Serial.println("Closing right drawer");
  moveServo(rightDrawerServo, 130, rightMin, rightMax);
}

// Causes the servo to move such that the key drawer opens
void openKeyDrawer(){
  Serial.println("Opening key drawer");
  moveServo(keyDrawerServo, 180, keyMin, keyMax);
}
// Causes the servo to move such that the key drawer opens
void closeKeyDrawer(){
  Serial.println("Closing key drawer");
  moveServo(keyDrawerServo, 0, keyMin, keyMax);
}

// Causes the servo to move such that the right drawer closes
void resetServos(){
  Serial.println("Closing both drawers");
  closeRightDrawer();
  closeKeyDrawer();
}


Would anyone happen to know how I can fix this? Your answer is highly appreciated!

PieterP

Please read this and edit your original post to format the code correctly using [code][/code] tags.

The error says it all: you're trying to use a variable that you did not declare. ServoPin is only used once in your code, and it has no declaration.

Pieter

sanderpander

Hi Pieter, thank you for your reply.

To declare it, could I do something like this?

Code: [Select]
void moveServo(Servo servo, int position, int servoPin, int minVal, int maxVal) {
  servo.write(position);
  servo.attach(servoPin, minVal, maxVal);
  delay(500);
  servo.detach();
}

gfvalvo

That would be better.

But:

* I haven't used the Servo library. But, I'm wondering does it make sense to write() to it BEFORE your attach() it? I'd have thought it would be the other way around.

* IMO, it's inefficient (and inelegant) to pass a Servo object to your function. That forces a whole new Servo object to be copy-constructed onto the stack just so you can attach it, move it, then detach it. Then, the object must be destructed. Much better to pass a pointer (or reference) to the Servo object(s) that you've already defined.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

septillion

* I haven't used the Servo library. But, I'm wondering does it make sense to write() to it BEFORE your attach() it? I'd have thought it would be the other way around.
It does, if you don't set it to another angle it will go to 90 degree's otherwise. It's similar like calling a digitalWrite(pin, HIGH) before a pinMode(pin, OUTPUT) :)
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

gfvalvo

OK, thanks. I assume you mean: "if you don't set it to another angle it will go to 90 degree's otherwise when first attached"?
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

septillion

Uhm, yes indeed :) Once you attach it, a position signal is send. It's not possible to send a "no position command" to a servo. The Servo library initializes that to 90 degree.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

sanderpander

#7
Jan 19, 2019, 03:07 pm Last Edit: Jan 19, 2019, 03:08 pm by sanderpander
Thank you for all answers, I now have a similar error, do anyone happen to know where this should be declared?

Error code:

Code: [Select]

                ^

sketch\PegBoard.cpp: In function 'Connection scanPins()':

PegBoard.cpp:25:28: error: 'pegCount' was not declared in this scope

         for (byte in=0; in<pegCount; in++) {

                            ^


This is the reference code:

Code: [Select]

#include "PegBoard.h"
 
// Constructor
PegBoard::PegBoard(const byte numPegs, const byte pins[]) {
    pegCount = numPegs;
    pegPins = new byte[pegCount];
    for(int i=0; i<pegCount; i++){
        pegPins[i]=pins[i];
    }
    lastConn = Connection();
}

// Destructor
PegBoard::~PegBoard() {
    delete [] pegPins;
}

/*
 * Scans hardware, returns a connection
 * object representing the pins (if any)
 * that are connected
 */
Connection scanPins() {
      // One by one, pick a starting peg
        for (byte in=0; in<pegCount; in++) {
        startProbe(in);
        // One by one, pick every OTHER peg
        for (byte out=0; out<pegCount; out++) {
          // Don't consider the same peg simultaneously as both input and output
          if (in == out) {
            continue;
          }
          if (!digitalRead(pegPins[out])) {
            // We've seen a connected pair of pegs
            stopProbe(in);
            return Connection(in, out);
          }
        }
        // No connections seen using this peg
        stopProbe(in);
      }
      // No pins are connected
      return Connection();
    }

void startProbe(byte in) {
    // Set it as output
    pinMode(pegPins[in],OUTPUT);
    // Tie that peg to ground
    digitalWrite(pegPins[in], LOW);
}

void stopProbe(byte in) {
    // Send a 5v signal on this pin
    digitalWrite(pegPins[in],HIGH);
    // Set it as an input, with pullup resistor
    pinMode(pegPins[in],INPUT_PULLUP);
}

// Returns true, if there's a connection
// (different than the last seen)
// Otherwise, returns false.
boolean hasNewConnection() {
    // Scan the hardware for connections
    Connection currentConn = scanPins();

    if (currentConn.isConnected()) {
        // Same as the last one
        if( currentConn == lastConn ) {
            return false;
        }
        // A new connection has been established
        else {
            lastConn = currentConn;
            return true;
        }
    }
    // No connection present
    else {
        return false;
    }
}

Connection getConnection() {
    return lastConn;
}


Thank you for your knowledge and time.

septillion

#8
Jan 19, 2019, 03:36 pm Last Edit: Jan 19, 2019, 03:37 pm by septillion
That's not all your code. Missing a .h and a sketch to use it in See http://snippets-r-us.com/  ;)

But again, the error is clear, you never declared 'pegCount' anywhere. And I have to agree with the compiler. Although I don't have all the code ;)
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

wildbill

It looks like you forgot to make scanPins a member of the PegBoard class. Compare its declaration to that of the constructor.

sanderpander

Thank you for valuable information. I did not write this code, unfortunately. I would be happy to make the scanPins a member of the PegBoard class, if I had the knowledge to do so.

Could you give me a hint on where and how I can do this?

Thanks again for your knowledge and time.

septillion

Could you give me a hint on where and how I can do this?
=>
Compare its declaration to that of the constructor.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

sanderpander

Hi, would this be the correct way to add the scanPins to the PegBoard class?

Code: [Select]

// Constructor
PegBoard::PegBoard(const byte numPegs, const byte scanPins, const byte pins[]) {
    pegCount = numPegs;
    pegPins = new byte[pegCount];
    for(int i=0; i<pegCount; i++){
        pegPins[i]=pins[i];
    }
    lastConn = Connection();
}

wildbill

No. You need to give the function a PegBoard:: prefix to tell the compiler it's part of the class. Probably have to make a change in the .h file too. Best post that too.

sanderpander

Thank you for helping out. Here's the PegBoard.h reference:

Code: [Select]

#ifndef PEGBOARD
#define PEGBOARD
 
#include <Arduino.h>
#include "Connection.h"
 
/*
  Defines a Peg Board hardware configuration.

  Knows how to interface with the hardware to determine
  current connections, and detect change.
*/

class PegBoard {
  private:
    byte pegCount;
    byte* pegPins;
    Connection lastConn;

    // Scans hardware, returns a connection
    // object representing the pins (if any)
    // that are connected
    Connection scanPins();

    // Sets pin for scan mode
    void startProbe(const byte pin);

    // Sets pin back to normal mode
    void stopProbe(const byte pin);
 
  public:
    // Constructor
    PegBoard(const byte numPegs, const byte pins[]);
    // Destructor
    ~PegBoard();

    // Returns true, if there's a connection
    // (different than the last seen)
    // Otherwise, returns false.
    boolean hasNewConnection();

    // Returns the most recent connection
    Connection getConnection();
};
 
#endif

Go Up