Turning on/off continuous sweep servo

What do you mean the failure started there? Is that the line that Sketch said there was an error on? What error?

You may be throwing everyone off when you're pointing us to 'where' the failure starts when in reality the failure is elsewhere because of where it references to.

INTP: What do you mean the failure started there? Is that the line that Sketch said there was an error on? What error?

You may be throwing everyone off when you're pointing us to 'where' the failure starts when in reality the failure is elsewhere because of where it references to.

The program that I have been using highlighted the text in the line, along with the other functions I was trying to make, and gave me the error saying "not declared in this scope".

Why have your function declarations got semicolons at the end ?

UKHeliBob:
Why have your function declarations got semicolons at the end ?

Thanks for noticing that, but I am still getting the error in the loop section saying that the function is not declared.

See attached image.

Sketch.png

The semi-colons were the problem. It compiles fine in Sketch when you remove them. Must be a side effect of whatever not-Sketch program you are using, that is causing it to not acknowledge the checkButtons as a void, it's expecting a data type.

INTP: The semi-colons were the problem. It compiles fine in Sketch when you remove them. Must be a side effect of whatever not-Sketch program you are using, that is causing it to not acknowledge the checkButtons as a void, it's expecting a data type.

:open_mouth: You're right. I just tried it on another online coding programmer and it worked. It didn't fail. Weird.

Thanks for your help on that.

That error message looks very odd to me.

Which version of the IDE are you running ?

UKHeliBob:
That error message looks very odd to me.

Which version of the IDE are you running ?

IDE? I have been using a website online to work on the programming. Here is the link for my project on the website:

You can view the current code and test the programming. I can get you the license, too, once the website stops acting up and loads the page.

I also got the servo to start with the button but I can’t get it to stop when I press the button again. I have hear just about everywhere I look that I need to use millis() instead of delay(), I just don’t know how to write it.

//Add Servo library
#include <Servo.h>

//List Servo names
  Servo eyeservo, tailservo;

//Set up Constant Pins
  const int actPin = 3; //Activation Button
  const int output1Pin = 9; // Eye Servo
  const int output2Pin = 6; // Tail Servo
  const int ledPin = 13; //Test LED

//Positions for Tail Servo
  const byte maxTDeg = 160;
  const byte minTDeg = 45;
  byte tailPos = minTDeg;

//Positions for Eye Servo
  const int maxEDeg = 179;
  const int minEDeg = 1;
  byte eyePos = maxEDeg;

void setup()
{
//Attach Servos to pins
  eyeservo.attach(output1Pin);
  tailservo.attach(output2Pin);
  
//Move Servos to starting positions
  tailservo.write(tailPos);
  eyeservo.write(eyePos);
  delay(5000);

//Set up pinModes
  pinMode(actPin, INPUT_PULLUP);
  pinMode(output1Pin, OUTPUT);
  pinMode(output2Pin, OUTPUT);
  pinMode(ledPin, OUTPUT);

}

void loop()
{

  //read the pushbutton value into a variable
  int sensorVal = digitalRead(3);
  //print out the value of the pushbutton
  Serial.println(sensorVal);

  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
  } else {
    digitalWrite(13, HIGH);
  }

//Tail Sweep Program
  if(sensorVal == LOW){ //Has the button been pressed?
    for(tailPos = 0; tailPos < 160; tailPos += 1)
  {
    tailservo.write(tailPos);
    delay(15);
  }
    for(tailPos = 160; tailPos>=1; tailPos-=1)
  {
    tailservo.write(tailPos);
    delay(15);
  }
 }
  else
  {
    if(sensorVal == HIGH){
    tailservo.write(90);
    delay(15);
    }
  }
}

I have been using a website online to work on the programming.

Why not download and use the IDE ? It is, after all, free !

UKHeliBob: Why not download and use the IDE ? It is, after all, free !

I have been doing most of my programming in between projects at work and I can't download programs to this computer, hence the reason for using the website.

DracoAnimatronica: I also got the servo to start with the button but I can't get it to stop when I press the button again. I have hear just about everywhere I look that I need to use millis() instead of delay(), I just don't know how to write it.

Can someone help me with this? I haven't made any changes to my code since I last posted it.

Thanks

Can probably just define a new variable as 'servoState', treat it like an on/off led. Push button first time, servo stuff turns on, but also sets servoState to 1/0, HIGH/LOW, TRUE/FALSE, whatever you wanna call it. Push button second time, will change its state and use that variable to command servo.

INTP: Can probably just define a new variable as 'servoState', treat it like an on/off led. Push button first time, servo stuff turns on, but also sets servoState to 1/0, HIGH/LOW, TRUE/FALSE, whatever you wanna call it. Push button second time, will change its state and use that variable to command servo.

I did that but it doesn't work. Plus I want the servo to return to its starting point when the button is pressed.

Try this

unsigned long sweepStepStartTime;
unsigned long sweepStepInterval = 15;
unsigned long currentTime;
const byte buttonPin = A1;
byte currentButtonState;
byte previousButtonState = HIGH;
boolean sweeping = false;
int servoPos = 0;
int servoStepSize = 1;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
  currentButtonState = digitalRead(buttonPin);
  if (currentButtonState != previousButtonState)
  {
    if (currentButtonState == LOW)  //button has become pressed
    {
      Serial.println("Button press");
      sweeping = !sweeping;  //change state
    }
  }

  if (!sweeping)  //stopped sweeping
  {
    servoPos = 0;
  }
  else  //sweeping active
  {
    currentTime = millis();
    if (currentTime - sweepStepStartTime >= sweepStepInterval)  //time to move ?
    {
      sweepStepStartTime = currentTime;    //note the time the servo moved
      servoPos += servoStepSize;          //update the servo position variable
      if (servoPos <= 0 || servoPos >= 180)    //end of servo travel
      {
        servoStepSize = -1 * servoStepSize;  //reverse the servo
      }
    }
  }
  Serial.print("Servo is at position ");
  Serial.println(servoPos);
  previousButtonState = currentButtonState;    //ready for the next check
}

You will need to add code to actually move the servo and it would be a good idea add code to debounce the switch too. You will, of course, also need to change the pin number and maybe the logic to detect a button press to match your setup.

DracoAnimatronica: Can someone help me with this? I haven't made any changes to my code since I last posted it.

How about updating the code you posted in Reply #19 to take account of the comments you received and then see if that works.

...R

Robin2: How about updating the code you posted in Reply #19 to take account of the comments you received and then see if that works.

...R

Please see Reply #27 for most recent code.

DracoAnimatronica: Please see Reply #27 for most recent code.

I had seen that before I wrote Reply #34

...R

Hi Robin2, INTP, and UKHeliBob. Just thought I would put an updated here for you guys who were trying to help me. I did manage to figure out why the programming that I was trying to use from post #19 wasn’t working. I didn’t know that I needed to have programming in each of the functions for the program to stop failing. I am getting closer to having what I want to run but I still have a ways yet to go.

Anyways here’s what I currently have:

// Libraries
#include <Servo.h>

//======================

// Constants

 //Pins
  const int actPin = 7; //Activation Button
  const int onBoardLedPin = 13; //Test LED
  const int output1Pin = 9; // Eye Servo Pin
  const int output2Pin = 6; // Tail Servo Pin
  
 //Degrees for Eye servo
  const int maxEDeg = 90;
  const int minEDeg = 1;

 //Degrees for Tail servo
  const byte maxTDeg = 160;
  const byte minTDeg = 0;

 //Item Intervals & Durations
  const int onBoardLedInterval = 500; // number of millisecs between blinks
  const int buttonInterval = 300; // number of millisecs between button readings
  const int blinkDuration = 500; // number of millisecs that Led is on


//======================

// Variables

 //Call out servo names
  Servo eyeservo, tailservo;

 //Servo Starting positions 
  int eyePos = 179;
  int tailPos = 1;

  int servoSlowInterval = 10; // millisecs between servo moves
  int servoFastInterval = 10;
  int servoInterval = servoSlowInterval; // initial millisecs between servo moves
  int servoDegrees = 2;       // amount servo moves at each step 
                              // will be changed to negative value for movement in the other direction

//Button variable (will be used to tell the servos to run the sleep program)
  int push = 0;

 //Item States

 byte onBoardLedState = LOW; // used to record whether the LED is on or off
 byte buttonLed_State = LOW; //used to record whether the button is pushed or not

 //Millis()
  unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
  unsigned long previousOnBoardLedMillis = 0;   // will store last time the LED was updated
  unsigned long previousButtonMillis = 0; // time when button press last checked
  unsigned long previousEServoMillis = 0; // the time when the Eye servo was last moved
  unsigned long previousTServoMillis = 0; // the time when the Tail servo was last moved

//======================

void setup()
{
  
 Serial.begin(9600);
 Serial.println("Starting DragonModes.ino");  // so we know what sketch is running
  
 //Set up OUTPUT Pins
  pinMode(onBoardLedPin, OUTPUT);
  
 //Set up Button INPUT Pin
  pinMode(actPin, INPUT_PULLUP);

 //Set up Servos
  eyeservo.write(eyePos);
  eyeservo.attach(output1Pin);
  tailservo.write(tailPos);
  tailservo.attach(output2Pin);

}

//=======================

void loop()
{
  //Millis()
  currentMillis = millis();
  
 //Call out functions
  checkButton();
  Power();
  setLED();
  servoTailSweep();
//  servoEyeBlink();
  
}

//=======================

void checkButton() 
{ //check the state of the button
 
 if (millis() - previousButtonMillis >= buttonInterval)
 {
  if (digitalRead(actPin) == LOW)
  {
   buttonLed_State = ! buttonLed_State;
   previousButtonMillis += buttonInterval;
  }
 }
  //Add program for push variable?
}

//=======================

void Power() 
{ //Turn on Programs

digitalWrite(onBoardLedPin, onBoardLedState); //On board light
//Tail Servo (to be written)
//Eye Servo (to be written)

}

//=======================
void setLED()
{ //Set LED state based on button

  if (onBoardLedState == LOW)
  {
    if (currentMillis - previousOnBoardLedMillis >= onBoardLedInterval) {
       onBoardLedState = HIGH;
       previousOnBoardLedMillis += onBoardLedInterval;
    }
  }
  else
 {
  
    if (currentMillis - previousOnBoardLedMillis >= blinkDuration) {
       onBoardLedState = LOW;
       previousOnBoardLedMillis += blinkDuration;
    } 
  }
}

//=======================

void servoTailSweep()
{  //Start the Tail Sweep servo  
  
 if (currentMillis - previousTServoMillis >= servoInterval)
 {
    previousTServoMillis += servoInterval;
    tailPos = tailPos + servoDegrees;
    
  if (tailPos <= minTDeg)
  {
   if (servoInterval == servoSlowInterval)
   {
       servoInterval = servoFastInterval;
   }
    else
    {
        servoInterval = servoSlowInterval;
    }
  }

  if ((tailPos >= maxTDeg) || (tailPos <= minTDeg))  
  {  
      servoDegrees = - servoDegrees; 
      tailPos = tailPos + servoDegrees; 
  }
    tailservo.write(tailPos);
 }
}

//===============================

//void servoEyeBlink() 
//{ //Start the Eye Blink servo

  //I haven't written the program for this yet

//}

//=======================

DracoAnimatronica: Anyways here's what I currently have:

Glad to hear you are making progress. Let us know when you need more assistance.

...R

Robin2:
Glad to hear you are making progress.
Let us know when you need more assistance.

…R

Thanks. I actually have been using the “several things at the same time” coding to get things working but I actually am kind of confused about how the LED’s in the program know if the button was pushed or not. I don’t understand what links them to each other. Its quiet puzzling to me.