How to make a loop repeat prints until another button is pressed?

I'm trying to make a numbers ladder, and i don't know how to make it loop without holding the button. I also don't know hot make it break from the loop when I press another button. Can someone help me with that?

int boton1 = 13; 
int boton2 = 12;
int boton3 = 11;
int boton4 = 10; 
int boton5 = 9;
int boton6 = 8;
int boton7 = 7; 
int boton8 = 6;
int boton9 = 5;
int boton10 = 4;
int n = 0;

void setup(){
  pinMode (boton1, INPUT);
  pinMode (boton2, INPUT);
  pinMode (boton3, INPUT);
  pinMode (boton4, INPUT);
  pinMode (boton5, INPUT);
  pinMode (boton6, INPUT);
  pinMode (boton7, INPUT);
  pinMode (boton8, INPUT);
  pinMode (boton9, INPUT);
  pinMode (boton10, INPUT);
  Serial.begin(9600);
}

void loop(){

  if(digitalRead(boton1) == HIGH){
    for (int i = 0; i <= 255; i++) {
    Serial.println("1");
    delay(1000);
    }
  }
  
  if(digitalRead(boton2) == HIGH){
    do {
      Serial.println("1");
      delay(500);
      Serial.println("22");
      delay(1000);}
    while(digitalRead(boton2) == HIGH);
  }
  
  if(digitalRead(boton3) == HIGH){
    do {
      Serial.println("1");
      delay(500);
      Serial.println("22");
      delay(500);
      Serial.println("333");
      delay(1000);}
    while(digitalRead(boton3) == HIGH);
  }
  
  if(digitalRead(boton4) == HIGH){
    do{
      Serial.println("1");
      delay(500);
      Serial.println("22");
      delay(500);
      Serial.println("333");
      delay(500);
      Serial.println("4444");
      delay(1000);}
    while(digitalRead(boton4) == HIGH);
  }
  
  if(digitalRead(boton5) == HIGH){
    do{
      Serial.println("1");
      delay(500);
      Serial.println("22");
      delay(500);
      Serial.println("333");
      delay(500);
      Serial.println("4444");
      delay(500);
      Serial.println("55555");
      delay(1000);}
    while(digitalRead(boton6) == HIGH);
  }
  
  if(digitalRead(boton6) == HIGH){
    do{
      Serial.println("1");
      delay(500);
      Serial.println("22");
      delay(500);
      Serial.println("333");
      delay(500);
      Serial.println("4444");
      delay(500);
      Serial.println("55555");
      delay(500);
      Serial.println("666666");
      delay(1000);}
    while(digitalRead(boton6) == HIGH);
  }
  
  if(digitalRead(boton7) == HIGH){
    do {
      Serial.println("1");
      delay(500);
      Serial.println("22");
      delay(500);
      Serial.println("333");
      delay(500);
      Serial.println("4444");
      delay(500);
      Serial.println("55555");
      delay(500);
      Serial.println("666666");
      delay(500);
      Serial.println("7777777");
      delay(1000);}
    while(digitalRead(boton7) == HIGH);
  }
  
  if(digitalRead(boton8) == HIGH){
    do{
      Serial.println("1");
      delay(500);
      Serial.println("22");
      delay(500);
      Serial.println("333");
      delay(500);
      Serial.println("4444");
      delay(500);
      Serial.println("55555");
      delay(500);
      Serial.println("666666");
      delay(500);
      Serial.println("7777777");
      delay(500);
      Serial.println("88888888");
      delay(1000);}
    while(digitalRead(boton8) == HIGH);
  }
  
  if(digitalRead(boton9) == HIGH){
    do{
 	  Serial.println("1");
      delay(500);
      Serial.println("22");
      delay(500);
      Serial.println("333");
      delay(500);
      Serial.println("4444");
      delay(500);
      Serial.println("55555");
      delay(500);
      Serial.println("666666");
      delay(500);
      Serial.println("7777777");
      delay(500);
      Serial.println("88888888");
      delay(500);
      Serial.println("999999999");
      delay(1000);}
    while(digitalRead(boton9) == HIGH);
  }
  
  if(digitalRead(boton10) == HIGH){
    do{
      Serial.println("1");
      delay(500);
      Serial.println("22");
      delay(500);
      Serial.println("333");
      delay(500);
      Serial.println("4444");
      delay(500);
      Serial.println("55555");
      delay(500);
      Serial.println("666666");
      delay(500);
      Serial.println("7777777");
      delay(500);
      Serial.println("88888888");
      delay(1000);
      Serial.println("999999999");
      delay(500);
      Serial.println("10101010101010101010");
      delay(1000);}
    while(digitalRead(boton10) == HIGH);
  }
}

What is a numbers ladder?`

The purpose of loop() is to repeat, so there is no breaking out of loop()

Basically it prints out a number, 1, 22, 333 and so on. It's not a weird thing. I only need a way to loop it out until a button is pressed and does other print pattern

You can refer to Arduino how to use button to start/stop the loop

What does this mean?

I'm sorry, my english isn't so good.

I'm trying to say that I want the ladder to change when i press another button.

Example, I press the third button (1,22,333) and I press the fifth button (1,22,333,4444,55555), I just want a solution to make it that the third sequence stops and the fifth one starts.

Hello begforthetruecode7707

What is the task of this sketch in real life?

It's mostly just homework for a class I have that uses arduino, no big use irl. Only virtual

Post the assignment as you received it. It may be easier to understand than trhan your describe of what your goal is.

a7

Your English is good. I just do not understand the phrases you are trying to use. Write in your natural language and paste it into translate.google.com.

Hello begforthetruecode7707

Take a view to this small example for your own code studies.

//https://forum.arduino.cc/t/how-to-make-a-loop-repeat-prints-until-another-button-is-pressed/1235773/1
#define ProjectName "How to make a loop repeat prints until another button is pressed?"
#define NotesOnRelease "Arduino MEGA tested"
// make names
enum TimerEvent {NotExpired, Expired};
enum TimerControl {Halt, Run};
enum ButtonAction {Released, Pressed};
enum ButtonName {One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,None};
// make variables
uint32_t currentMillis = millis();
const String text[] {
  {"1"},
  {"22"},
  {"333"},
  {"4444"},
  {"5555"},
  {"66666"},
  {"777777"},
  {"8888888"},
  {"99999999"},
  {"10101010101010101010"},
};
//constexpr uint8_t ButtonPins[] {A0, A1, A2, A3, A4, A5};
constexpr uint8_t ButtonPins[] {13,12,11,10,9,8,7,6,5,4};

// make structures
struct TIMER
{
  uint32_t interval;
  uint8_t control;
  uint32_t now;
  void make (uint32_t interval_)
  {
    interval = interval_;
  }
  void launch(uint32_t currentMillis)
  {
    control = Run;
    now = currentMillis;
  }
  uint8_t expired(uint32_t currentMillis)
  {
    uint8_t timerEvent = currentMillis - now >= interval and control;
    if (timerEvent == Expired) now = currentMillis;
    return timerEvent;
  }
};
TIMER blinkMe;
struct BUTTON
{
  uint8_t name;
  uint8_t pin;
  uint8_t stateOld;
  TIMER debounce;
  void make(uint8_t name_, uint8_t pin_)
  {
    name = name_;
    pin = pin_;
    pinMode(pin, INPUT_PULLUP);
    stateOld = digitalRead(pin) ? LOW : HIGH;
    debounce.now = currentMillis;
    debounce.interval = 20;
    debounce.control = Run;
    Serial.print (__func__), Serial.print (" "), Serial.println (pin);
  }
  uint8_t read()
  {
    uint8_t returnValue = None;
    if (debounce.expired(currentMillis) == Expired)
    {
      uint8_t stateNew = digitalRead(pin) ? LOW : HIGH;
      if (stateOld != stateNew)
      {
        stateOld = stateNew;
        if (stateNew == Pressed) returnValue = name;
      }
    }
    return returnValue;
  }
} buttons[sizeof(ButtonPins)];
// make support
void heartBeat(const uint8_t LedPin, uint32_t currentMillis)
{
  static bool setUp = false;
  if (setUp == false) pinMode (LedPin, OUTPUT), setUp = true;
  digitalWrite(LedPin, (currentMillis / 500) % 2);
}
// make application
void setup()
{
  Serial.begin(115200);
  Serial.print("Source: "), Serial.println(__FILE__);
  Serial.print(ProjectName), Serial.print(" - "),  Serial.println(NotesOnRelease);
  blinkMe.make(500);
  blinkMe.launch(millis());
  uint8_t element = 0;
  for (auto &button : buttons)
  {
    button.make(element, ButtonPins[element]);
    element++;
  }
  Serial.println(" =-> and off we go\n");
}
void loop()
{
  currentMillis = millis();
  static uint8_t buttonPressed = 0;
  static uint8_t stringAddress = 0;
//  heartBeat(LED_BUILTIN, currentMillis);
  for (auto &button : buttons)
  {
    if (button.read() != None)
    {
      buttonPressed = button.name;
      stringAddress = 0;
    }
  }
  if (blinkMe.expired(currentMillis) == Expired)
  {
    Serial.println(text[stringAddress]);
    stringAddress++;
    if (stringAddress > buttonPressed ) stringAddress = 0; ;
    if (stringAddress == 0) Serial.println("");
  }
}

Have a nice day and enjoy coding in C++.

A slightly different solution:

//////////////////////////////////////////////////////////////////////////////
/// @brief Helper class for debouncing input signals (e.g. push-buttons)
///
//////////////////////////////////////////////////////////////////////////////
using uint = unsigned int;
using ulong = unsigned long;

class Debounce {
public:
  Debounce(unsigned int dbt) : debounceTime {dbt} {}
  bool operator()(const uint8_t, const uint8_t);

private:
  uint debounceTime;
  uint8_t prevPinState;
  ulong timeStamp {0};
};

bool Debounce::operator()(const uint8_t pin, uint8_t activePinState = LOW) {
  bool isDebounceReady = false;
#ifdef __digitalWriteFast_h_
  uint8_t pinState = digitalReadFast(pin);
#else
  uint8_t pinState = digitalRead(pin);
#endif
  if (pinState != prevPinState) {   // Restart the debounce timer with every edge change
    timeStamp = millis();
    prevPinState = pinState;
  } else if (pinState == activePinState && (millis() - timeStamp >= debounceTime)) {
    isDebounceReady = true;   // No more edge changes within the debounce time but the pinState is permanently unequal
                              // to the previous one before the test.
  }
  return isDebounceReady;
}

// ------------------- Helper end ----------------------------------------------------

// #define CONTINUOUS

constexpr uint8_t BUTTON_NUM {10};
constexpr uint8_t btn[BUTTON_NUM] {13, 12, 11, 10, 9, 8, 7, 6, 5, 4};

// One debounce object for each button with 15ms debounce time
Debounce debounce[BUTTON_NUM] {{15}, {15}, {15}, {15}, {15}, {15}, {15}, {15}, {15}, {15}};

void printLadder(uint8_t num) {
  for (size_t y = 0; y < num; ++y) {
    for (size_t i = 0; i <= y; ++i) { Serial.print(num); }
    Serial.println();
  }
  Serial.println("-");
  delay(500);
}

void setup() {
  Serial.begin(115200);
  for (auto b : btn) { pinMode(b, INPUT_PULLUP); }
  Serial.println("To start press a button...");
}

void loop() {
  static uint8_t number {0};
  static uint8_t prevNumber {0};
  for (uint8_t i = 0; i < BUTTON_NUM; ++i) {
    if (number == prevNumber && true == debounce[i](btn[i])) {
      number = i + 1;
    } else if (number != prevNumber) {
      prevNumber = number;
      printLadder(number);
#ifdef CONTINUOUS 
      number = prevNumber = 0;
#endif
    }
  }
}

To try out

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.