Arduino sequencer

I have the following code:

// Pin definitions
#define CLOCK_SWITCH_PIN 2      // Clock on/off SPST switch
#define STEP_SELECT_PIN 3       // Step selection SPDT switch common terminal
#define POT_PIN A4              // Potentiometer for clock rate
#define ONBOARD_LED_PIN 13      // Onboard LED pin
int ledPins[] = {4, 5, 6, 7, 8, 9};  // Output pins for LEDs/relays

// Variables
int currentStep = 0;            // Current step in the sequence
unsigned long lastStepTime = 0; // Last time a step was updated
unsigned long onboardLedTime = 0; // Last time the onboard LED was toggled
int stepDelay = 500;            // Default delay (ms)
int totalSteps = 6;             // Default number of steps
bool clockRunning = false;      // Clock on/off state
bool onboardLedState = false;   // State of the onboard LED

unsigned long previousMillis = 0; // Stores the last time the step was updated
unsigned long previousLedMillis = 0; // Stores the last time the onboard LED toggled

void setup() {
  // Configure input pins
  pinMode(CLOCK_SWITCH_PIN, INPUT_PULLUP); // Enable pull-up for clock switch
  pinMode(STEP_SELECT_PIN, INPUT_PULLUP);  // Enable pull-up for SPDT switch

  // Configure LED output pins
  for (int i = 0; i < 6; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW);  // Ensure all outputs start LOW
  }

  // Configure the onboard LED pin
  pinMode(ONBOARD_LED_PIN, OUTPUT);
  digitalWrite(ONBOARD_LED_PIN, LOW); // Ensure the onboard LED starts OFF
}

void loop() {
  // Read the state of the clock switch
  clockRunning = !digitalRead(CLOCK_SWITCH_PIN); // LOW = clock ON, HIGH = clock OFF

  // Read the step selector switch state
  int stepSelectorState = digitalRead(STEP_SELECT_PIN);
  if (stepSelectorState == LOW) {
    totalSteps = 5; // Switch connected to GND → 5 steps
  } else {
    totalSteps = 6; // Switch in HIGH position → 6 steps
  }

  // Read the potentiometer to set step delay
  int potValue = analogRead(POT_PIN);
  stepDelay = map(potValue, 0, 1023, 50, 1500); // Map pot to delay range (100ms to 2s)

  // If the clock is running, handle the sequencer logic
  if (clockRunning) {
    // Check if it's time to update the step
    if (millis() - previousMillis >= stepDelay) {
      previousMillis = millis(); // Update the time

      // Turn off all LEDs
      for (int i = 0; i < 6; i++) {
        digitalWrite(ledPins[i], LOW);
      }

      // Activate the current step LED
      digitalWrite(ledPins[currentStep], HIGH);

      // Move to the next step
      currentStep = (currentStep + 1) % totalSteps;
    }

    // Blink the onboard LED at the clock speed
    if (millis() - previousLedMillis >= stepDelay) {
      previousLedMillis = millis(); // Update the onboard LED toggle time
      onboardLedState = !onboardLedState; // Toggle onboard LED state
      digitalWrite(ONBOARD_LED_PIN, onboardLedState);
    }
  } else {
    // Ensure all LEDs are LOW and onboard LED is OFF when the clock is off
    for (int i = 0; i < 6; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    digitalWrite(ONBOARD_LED_PIN, LOW);
    onboardLedState = false; // Reset the onboard LED state
  }
}

The problem is the output of the arduino is only 5V and I would like to generate 9V output.
I thought I should use a transistors but not sure how I should wire them?

Edit - added schematics

Do you mean that you want to use 9V for the LEDs?

You can use a MOSFTET like this,

Ref: GroundFungus

Yes, kind of. instead of the leds I am feeding this output to a capacitor to make an envelope to control the amplitude of 40106 osc. So 5V in too quite..
Here is my synth schematic(one part):


So I want to feed the output from the arduino sequencer to the juciton of D4 C4. 5V is not enough therfore I ask how to make it 9V

Here is my arduino schematics:

How can I make the voltage going across the diodes and the resistor to be 9V instead the current 5V?

Will this work?
when sequence output is LOW emmiter is tie to ground and therfore LOW as well. When sequence is HIGH - current will flow from 9V at the collector pin via R8 to the emmiter pin and via D1 and R1. Is this could work?

powering the arudino from 9V battery is not the best. I will power it from the onboard usb input.

Sorry, just curious about that extreme ratio 4M7/10k voltage divider at pin 9 of U1D?

That transistor is configured as an emitter follower.

The output voltage will be approximately 0.7V less than the input voltage i.e 4.3V, and not 9V.

to trigger that input by shorting the 10k resistor to ground with my finger

How should I set it then?

1 Like

That is great!!
My full arduino code:

#define BUTTON_PIN 3           // Button connected to pin 3
#define TIME_WINDOW 3000       // Time window in milliseconds (3 seconds)
#define DEBOUNCE_DELAY 200     // Debounce delay in milliseconds (200ms)

#define CLOCK_SWITCH_PIN 2     // Clock on/off SPST switch
#define POT_PIN A4             // Potentiometer for clock rate
#define ONBOARD_LED_PIN 13     // Onboard LED pin
int ledPins[] = {4, 5, 6, 7, 8, 9};  // Output pins for LEDs/relays

// Variables for button counting
unsigned long lastPressTime = 0;   // Time of the last button press
unsigned long pressStartTime = 0;   // Start time for press count
int buttonPressCount = 0;           // Number of presses within the time window
int finalButtonPressCount = 0;      // Store the final count of presses within the window
bool lastButtonState = HIGH;        // Last button state (default HIGH due to pull-up resistor)
bool buttonState = HIGH;            // Current button state

// Variables for sequencer
int currentStep = 0;                // Current step in the sequence
unsigned long lastStepTime = 0;     // Last time a step was updated
unsigned long onboardLedTime = 0;   // Last time the onboard LED was toggled
int stepDelay = 500;                // Default delay (ms)
int totalSteps = 6;                 // Default number of steps
bool clockRunning = false;          // Clock on/off state
bool onboardLedState = false;       // State of the onboard LED

unsigned long previousMillis = 0;   // Stores the last time the step was updated
unsigned long previousLedMillis = 0; // Stores the last time the onboard LED toggled

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // Set the button pin as input with pull-up
  pinMode(CLOCK_SWITCH_PIN, INPUT_PULLUP); // Enable pull-up for clock switch

  // Configure LED output pins
  for (int i = 0; i < 6; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW);  // Ensure all outputs start LOW
  }

  // Configure the onboard LED pin
  pinMode(ONBOARD_LED_PIN, OUTPUT);
  digitalWrite(ONBOARD_LED_PIN, LOW); // Ensure the onboard LED starts OFF

  Serial.begin(9600);  // Start serial communication at 9600 baud
}

void loop() {
  // Read the current state of the button
  buttonState = digitalRead(BUTTON_PIN);

  // If the button is pressed (state changed from HIGH to LOW)
  if (buttonState == LOW && lastButtonState == HIGH) {
    // Debounce the button (only count the press if enough time has passed)
    if (millis() - lastPressTime > DEBOUNCE_DELAY) {
      // Reset the press counter if a new press period starts
      if (millis() - pressStartTime > TIME_WINDOW) {
        pressStartTime = millis();  // Start a new counting period
        buttonPressCount = 0;       // Reset count
      }

      // Increment the press count
      buttonPressCount++;

      // Update the last press time to the current time
      lastPressTime = millis();
    }
  }

  // Store the current button state for the next loop iteration
  lastButtonState = buttonState;

  // If the time window has passed, reset the count and print it
  if (millis() - pressStartTime >= TIME_WINDOW) {
    if (buttonPressCount > 0) {
      finalButtonPressCount = buttonPressCount;  // Store the final press count within the window
      Serial.print("Final count within window: ");
      Serial.println(finalButtonPressCount);
    }
    pressStartTime = millis();  // Reset the timer for the next period
    buttonPressCount = 0;       // Reset the count after each period

    // Based on the final button presses, set the number of steps
    if (finalButtonPressCount == 1) {
      totalSteps = 3;  // If 1 press, 3 steps
    } else if (finalButtonPressCount == 2) {
      totalSteps = 4;  // If 2 presses, 4 steps
    } else if (finalButtonPressCount == 3) {
      totalSteps = 5;  // If 3 presses, 5 steps
    } else if (finalButtonPressCount >= 4) {
      totalSteps = 6;  // If 4 or more presses, 6 steps
    }
  }

  // Read the state of the clock switch
  clockRunning = !digitalRead(CLOCK_SWITCH_PIN); // LOW = clock ON, HIGH = clock OFF

  // Read the potentiometer to set step delay
  int potValue = analogRead(POT_PIN);
  stepDelay = map(potValue, 0, 1023, 50, 2000); // Map pot to delay range (100ms to 2s)

  // If the clock is running, handle the sequencer logic
  if (clockRunning) {
    // Check if it's time to update the step
    if (millis() - previousMillis >= stepDelay) {
      previousMillis = millis(); // Update the time

      // Turn off all LEDs
      for (int i = 0; i < 6; i++) {
        digitalWrite(ledPins[i], LOW);
      }

      // Activate the current step LED
      digitalWrite(ledPins[currentStep], HIGH);

      // Move to the next step
      currentStep = (currentStep + 1) % totalSteps;
    }

    // Blink the onboard LED at the clock speed
    if (millis() - previousLedMillis >= stepDelay) {
      previousLedMillis = millis(); // Update the onboard LED toggle time
      onboardLedState = !onboardLedState; // Toggle onboard LED state
      digitalWrite(ONBOARD_LED_PIN, onboardLedState);
    }
  } else {
    // Ensure all LEDs are LOW and onboard LED is OFF when the clock is off
    for (int i = 0; i < 6; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    digitalWrite(ONBOARD_LED_PIN, LOW);
    onboardLedState = false; // Reset the onboard LED state
  }
}

R1 can be 1k as well?

I wonder why my circuit did not worked when I connect my 9V battery to the Vin of the arduino nano? is it because there was not enough amp to deliver?

Yes

Without seeing the complete circuit I could only guess that it was not enough current or the battery was close to being dead

here is the full schematics:
Can this circuit could run on 9V battery or better to use the usb input? or an external 9V PSU into the Vin?

All depends. It may only last 4-5 hours with a 9V battery. If you want longer, then buy a 9V plug in adaptor and connect it to Vin and the rest of the circuit.

Any takes on my code? perhups things that can be simplified or change?

#define BUTTON_PIN 3        // Button connected to pin 3
#define TIME_WINDOW 3000    // Time window in milliseconds (3 seconds)
#define DEBOUNCE_DELAY 200  // Debounce delay in milliseconds (200ms)

#define CLOCK_SWITCH_PIN 2             // Clock on/off SPST switch
#define POT_PIN A4                     // Potentiometer for clock rate
#define ONBOARD_LED_PIN 13             // Onboard LED pin
int ledPins[] = { 4, 5, 6, 7, 8, 9 };  // Output pins for LEDs/relays

// Variables for button counting
unsigned long lastPressTime = 0;   // Time of the last button press
unsigned long pressStartTime = 0;  // Start time for press count
int buttonPressCount = 0;          // Number of presses within the time window
int finalButtonPressCount = 0;     // Store the final count of presses within the window
bool lastButtonState = HIGH;       // Last button state (default HIGH due to pull-up resistor)
bool buttonState = HIGH;           // Current button state

// Variables for sequencer
int currentStep = 0;               // Current step in the sequence
unsigned long lastStepTime = 0;    // Last time a step was updated
unsigned long onboardLedTime = 0;  // Last time the onboard LED was toggled
int stepDelay = 500;               // Default delay (ms)
int totalSteps = 6;                // Default number of steps
bool clockRunning = false;         // Clock on/off state
bool onboardLedState = false;      // State of the onboard LED

unsigned long previousMillis = 0;     // Stores the last time the step was updated
unsigned long previousLedMillis = 0;  // Stores the last time the onboard LED toggled

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);        // Set the button pin as input with pull-up
  pinMode(CLOCK_SWITCH_PIN, INPUT_PULLUP);  // Enable pull-up for clock switch

  // Configure LED output pins
  for (int i = 0; i < 6; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW);  // Ensure all outputs start LOW
  }

  // Configure the onboard LED pin
  pinMode(ONBOARD_LED_PIN, OUTPUT);
  digitalWrite(ONBOARD_LED_PIN, LOW);  // Ensure the onboard LED starts OFF

  Serial.begin(9600);  // Start serial communication at 9600 baud
}

void loop() {
  // Read the current state of the button
  buttonState = digitalRead(BUTTON_PIN);

  // If the button is pressed (state changed from HIGH to LOW)
  if (buttonState == LOW && lastButtonState == HIGH) {
    // Debounce the button (only count the press if enough time has passed)
    if (millis() - lastPressTime > DEBOUNCE_DELAY) {
      // Reset the press counter if a new press period starts
      if (millis() - pressStartTime > TIME_WINDOW) {
        pressStartTime = millis();  // Start a new counting period
        buttonPressCount = 0;       // Reset count
      }

      // Increment the press count
      buttonPressCount++;

      // Update the last press time to the current time
      lastPressTime = millis();
    }
  }

  // Store the current button state for the next loop iteration
  lastButtonState = buttonState;

  // If the time window has passed, reset the count and print it
  if (millis() - pressStartTime >= TIME_WINDOW) {
    if (buttonPressCount > 0) {
      finalButtonPressCount = buttonPressCount;  // Store the final press count within the window
      Serial.print("Final count within window: ");
      Serial.println(finalButtonPressCount);
    }
    pressStartTime = millis();  // Reset the timer for the next period
    buttonPressCount = 0;       // Reset the count after each period

    // Based on the final button presses, set the number of steps
    if (finalButtonPressCount == 1) {
      totalSteps = 3;  // If 1 press, 3 steps
    } else if (finalButtonPressCount == 2) {
      totalSteps = 4;  // If 2 presses, 4 steps
    } else if (finalButtonPressCount == 3) {
      totalSteps = 5;  // If 3 presses, 5 steps
    } else if (finalButtonPressCount >= 4) {
      totalSteps = 6;  // If 4 or more presses, 6 steps
    }
  }

  // Read the state of the clock switch
  clockRunning = !digitalRead(CLOCK_SWITCH_PIN);  // LOW = clock ON, HIGH = clock OFF

  // Read the potentiometer to set step delay
  int potValue = analogRead(POT_PIN);
  stepDelay = map(potValue, 0, 1023, 50, 2000);  // Map pot to delay range (100ms to 2s)

  // If the clock is running, handle the sequencer logic
  if (clockRunning) {
    // Check if it's time to update the step
    if (millis() - previousMillis >= stepDelay) {
      previousMillis = millis();  // Update the time

      // Turn off all LEDs
      for (int i = 0; i < 6; i++) {
        digitalWrite(ledPins[i], LOW);
      }

      // Activate the current step LED
      digitalWrite(ledPins[currentStep], HIGH);

      // Move to the next step
      currentStep = (currentStep + 1) % totalSteps;
    }

    // Blink the onboard LED at the clock speed
    if (millis() - previousLedMillis >= stepDelay) {
      previousLedMillis = millis();        // Update the onboard LED toggle time
      onboardLedState = !onboardLedState;  // Toggle onboard LED state
      digitalWrite(ONBOARD_LED_PIN, onboardLedState);
    }
  } else {
    // Ensure all LEDs are LOW and onboard LED is OFF when the clock is off
    for (int i = 0; i < 6; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    digitalWrite(ONBOARD_LED_PIN, LOW);
    onboardLedState = false;  // Reset the onboard LED state
  }
}