Varied speed iteration controller

Поділитися
Вставка
  • Опубліковано 17 жов 2024
  • Push button controls an iterative loop, the speed of the iteration is controlled by a potentiometer.
    Here is the code for your reference:
    /*
    Developed by Jesse McVaney
    Jan. 9, 2014
    A sketch for controlling a counter with a single button.
    When pushed the button triggers digital pin 2 to begin counting up.
    A potentiometer attached to analog pin 0 controls how quickly the upcount progresses
    through the use of the delay command. The potentiometer values are mapped into a usable range.
    */
    const int potPin = 0; // Potentiometer hooked to analong pin 2
    const int button = 2; // The button is outputting to pin 2
    int count = 0; // initialize the counter to zero
    int pot_value = 0; // The initial value on the potentiometer
    int delay_value = 0; // Allows for the map function to scale the potentiometer values
    void setup()
    {
    Serial.begin(9600); // Begin the serial communication
    //
    pinMode(button, INPUT); // Specify that the button on pin 2 is an input button
    }
    void loop ()
    {
    while (digitalRead(button) == HIGH) // while the button is pressed, perform the following loop
    {
    pot_value = analogRead(potPin);
    delay_value = map(pot_value,524,1023,0,1000);
    Serial.print("Iteration number = ");
    Serial.print(count++);
    Serial.print(" || Potentiometer value = ");
    Serial.print(pot_value);
    Serial.print(" || Delay value = ");
    Serial.println(delay_value);
    delay(delay_value);
    }
    }

КОМЕНТАРІ •