How to set servo position when using Sweeper class

Hey there. In my code below, could someone please tell me how to set the servo position to 100 right before the pro mini sleeps (at the bottom of the loop)? When I enter the usual code of “servo.write(100);” I get the error message…

exit status 1
‘myservo’ was not declared in this scope

#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <Servo.h>
//#include <avr/io.h>

//variables for the servo cut-off button
//int UsrBtn = 4; //Button to activate program loop
//int tranPin = 7; //Transister base pin

const int wakePin = 2;
const int LedPin1 = 11;
const int LedPin2 = 12;

//these values are for the PIRSense code in the loop
boolean lockLow = true;
boolean takeLowTime; 
long unsigned int lowIn;         
long unsigned int pause = 4000; 

//these values are for flashing the LEDs in the loop
//int ledState = LOW;
//long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//long interval = 6;  

//THIS LARGE SECTION IS FOR THE LEDS AND SERVOS TO MOVE SIMULTANEOUSLY DURING THE INTERRUPT
//*******************************************************************************************
class Flasher
{
  // Class Member Variables
  // These are initialized at startup
  int ledPin;      // the number of the LED pin
  long OnTime;     // milliseconds of on-time
  long OffTime;    // milliseconds of off-time
 
  // These maintain the current state
  int ledState;                 // ledState used to set the LED
  unsigned long previousMillis;   // will store last time LED was updated
 
  // Constructor - creates a Flasher 
  // and initializes the member variables and state
  public:
  Flasher(int pin, long on, long off)
  {
  ledPin = pin;
  pinMode(ledPin, OUTPUT);     
    
  OnTime = on;
  OffTime = off;
  
  ledState = LOW; 
  previousMillis = 0;
  }
 
  void Update()
  {
    // check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();
     
    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
      ledState = LOW;  // Turn it off
      previousMillis = currentMillis;  // Remember the time
      digitalWrite(ledPin, ledState);  // Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
      ledState = HIGH;  // turn it on
      previousMillis = currentMillis;   // Remember the time
      digitalWrite(ledPin, ledState);   // Update the actual LED
    }
  }
};
 
class Sweeper
{
  Servo servo;              // the servo
  int sleeppos;          // I made this one
  int pos;              // current servo position 
  int increment;        // increment to move for each interval
  int  updateInterval;      // interval between updates
  unsigned long lastUpdate; // last update of position
 
public: 
  Sweeper(int interval)
  {
    updateInterval = interval;
    increment = 1;
  }
  
  void Attach(int pin)
  {
    servo.attach(pin);
  }
  
  void Detach()
  {
    servo.detach();
  }
  
  void Update()
  {
    if((millis() - lastUpdate) > updateInterval)  // time to update
    {
      lastUpdate = millis();
      pos += increment;
      servo.write(pos);
      //Serial.println(pos);
      if ((pos >= 50) || (pos <= 30)) // end of sweep
      {
        // reverse direction
        increment = -increment;
      }
    }
  }
};
 
 
Flasher led1(11, 7, 2);
Flasher led2(12, 7, 2);
 
Sweeper sweeper1(10); //originally (100)
//Sweeper sweeper2(25);
//***********************************************************************************************

void setup() {


sweeper1.Attach(9);



// for the servo cut-off button
//pinMode(UsrBtn, INPUT);  //Set UsrBtn as an input
//pinMode(tranPin, OUTPUT); //Set tranPin as an output

  pinMode(wakePin, INPUT);
//    THIS WAS ALL IN VOID SLEEP NOW IN THE HUSH LITTLE ARDUINO INSTRUCTABLES - ADD THIS LATER
//    for (int i = 0; i < 20; i++) {
//    if(i != 11)//  because the LED/Relay is connected to digital pin 11
//    if(i != 12)//  because the LED/Relay is connected to digital pin 12
//    pinMode(i, INPUT);
  pinMode(LedPin1, OUTPUT);
  pinMode(LedPin2, OUTPUT);
  Serial.begin(115200);
  Serial.println("Warming up... wait for a min...");
  for( int i = 1; i <= 30; i++){
     digitalWrite(LedPin1, HIGH); delay(50); digitalWrite(LedPin2, LOW); delay(50);
     digitalWrite(LedPin2, HIGH); delay(50); digitalWrite(LedPin1, LOW); delay(50);
  }  
  Serial.println("Ready");
  attachInterrupt(0,wakeUpNow, RISING);

}

void sleepNow() {
  
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  ADCSRA &= ~(1 << 7); // disable ADC
  sleep_enable();
  sleep_bod_disable();
  attachInterrupt(0,wakeUpNow, RISING);
  sleep_mode();
  sleep_disable();
  detachInterrupt(0);

}

void wakeUpNow(){

}

void loop() {

  if(digitalRead(wakePin) == HIGH) {


//CODE FOR SERVO CUT-OFF BUTTON (got a funny smell, maybe from transistor - asked on Arduino forum (sort of) which transistor to use)
//int letMeBe = digitalRead(UsrBtn); //letMeBe is a digital read of the UsrBtn
//if (letMeBe == 1) //if letMeBe (UsrBtn) is high {
//digitalWrite(tranPin, HIGH); //make tranPin high
//}
//else if (letMeBe == 0) //else if letMeBe is low {
//digitalWrite(tranPin, LOW); //make the tranPin low
//servo.detach(); //stop sending pulses to reserve power on the Arduino.
//}


     //CODE FOR LEDS & SERVO(S) // BUT I SHOULD BE MAKING THEM FLASH & MOVE TO MUSIC!! MAKE IT SWITCHABLE?
     sweeper1.Update();
     //sweeper2.Update();
     led1.Update();
     led2.Update();
  
         if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         }         
         takeLowTime = true;
       }

     if(digitalRead(wakePin) == LOW){       
       digitalWrite(LedPin1, LOW);  //the led visualizes the sensors output pin state
       digitalWrite(LedPin2, LOW);
       
       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           digitalWrite(LedPin1, LOW);
           digitalWrite(LedPin2, LOW);
           //servo.write(100); //servo sleep/starting position
           delay(50); 
           sleepNow();
        }
     
     }
}

At no point in "loop()" is there anything called myservo or servo in scope.

I have the following in the loop

//servo.write(100); //servo sleep/starting position

When I remove the // it comes up with the error message I posted above. With the // inserted, no problems to speak of.

  Flasher(int pin, long on, long off)
  {
  ledPin = pin;
  pinMode(ledPin, OUTPUT);

You should NOT be diddling with the hardware in your constructor. The init() method has not been called yet, so the hardware is not ready, and any diddling you do will be undone when init() IS called.

bodkin77: When I remove the // it comes up with the error message I posted above.

No, it doesn't. Please don't waste my time.

Hi Paul

Sorry I do not understand what you are saying. That is because I don't understand any of the code that I 'lifted' from the last section of https://learn.adafruit.com/multi-tasking-the-arduino-part-1/a-clean-sweep.

There are classes and a public section etc. which I do not understand.

I do, however, understand the rest of the code.

I wouldn't expect someone to explain it all to me. I was just hoping that to begin with someone would tell how to move the servo to position 100 immediately before the Arduino sleeps (in my situation - the usual sitcho doesn't work here).

You haven't got anything called "servo" or "myservo" in scope anywhere in "loop()". I believe I have already mentioned this.

You do have something called "sweeper1" in scope.

When I reply I try to be considerate of people's time, but that includes my own time. I thought maybe you didn't notice the commented out line. I had a quick look at what scope means and maybe it means that my variable 'servo' is not globally defined, but defined only locally.

You mentioned sweeper1. Yes, the servo moves successfully with the command "sweeper1.Update()" near the top of the loop. Before I posted this question, I had already tried "sweeper1.write(100);" just prior to the sleep command. In that case it says

exit status 1 'class Sweeper' has no member named 'write'

I don't know how these classes and stuff work.

Why not just add a "park()" method to the Sweeper class?

Sorry, looked into it, but I'm all out of ideas. Found some park sweeping / street cleaning jobs. Not sure if they have night positions in my area?

Jokes aside, I think a method is a function. They are kind of confusing the heck out of me right now

"Sweeper::Attach", "Sweeper::Detach" and "Sweeper::Update" are all methods of the Sweeper class. "Sweeper::Sweeper" is a special method, the class constructor. (I think, strictly speaking, C++ doesn't have "methods", but it's fairly common parlance in OO circles).

It would be trivial to add a "Sweeper::Park" method to the Sweeper class.

void Park (int pos)
{
  servo.write (pos);
}

Somehow I feel I'm getting warmer. I'm not sure how to call that function/method and make it 100.

For clues, look at the other methods in the Sweeper class that you are calling.

That is because I don't understand any of the code that I 'lifted' from the last section of https://learn.adafruit.com

It would sure be nice if adafruit was more involved in this forum, AND submitted their code for review BEFORE posting crap on their site.

That's published code? :fearful:

AWOL: That's published code? :fearful:

That's what OP said.