Servo LED Speed Multiplier

Hey everyone!

First time posting on the forum, I’m just a 3D Printer / Prop builder, but I’m trying to dabble into adding electronics and I just have a couple questions that hopefully someone can help me with. I’ve attached the code down below ( Tutorial’d ) with some edits.

What I’m looking to do :

  • I’m wondering if when the Servo is travelling to it’s 110 increment, if I can put a speed multiplier to the leds , hopefully I can specify which ones )

If anyone could help me that would be wicked!

#include <Servo.h> 

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 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 >= 105) || (pos <= 0)) // end of sweep
      {
        // reverse direction
        increment = -increment;
      }
    }
  }
};
 
 
Flasher led1(11, 250, 250);
Flasher led2(12, 0, 0);
Flasher led3(13, 250, 252);

Sweeper sweeper1(15);
Sweeper sweeper2(25);
 
void setup() 
{ 
  Serial.begin(9600);
  sweeper1.Attach(9); // SERVO PIN
  sweeper2.Attach(10);
} 
 
 
void loop() 
{ 
  sweeper2.Update();
  
  if(digitalRead(2) == HIGH)  // IF BUTTON IS PUSHED ON PIN 2 , KEEP THESE SEQUENCES RUNNING TEMPORARILY 
  {
     sweeper1.Update();
     sweeper2.Update();
     led1.Update();
  }
  
  led2.Update(); // THE NORMAL SEQUENCES
  led3.Update();
}
long OnTime;     // milliseconds of on-time
  long OffTime;    // milliseconds of off-time

There are the two variables that control how long the light is on or off. I guess you could just modify those as needed to make the leds blink at whatever speed you'd like.

I guess the question is, how could I tell the Arduino to modify the timer based on the Servo’s position? I’m not really a coder so this has stumped me.

Thanks!

That’s an awfully complicated object oriented code for a noob. Maybe it would do you good to work on something simpler to start out.

Either way your Sweeper class holds the servo position in a variable named pos. Just write some if statements to look at that and calculate the intervals. Or if there is a mathematical relationship then write a line that does that math and sticks the result where you want it.