Switching the function using the "Switch" function

Hello everyone Can you please help with the code.
I wanted to add switching between functions to my project. I assume to use the "Switch" function for implementation. But I could not add to the code and make an algorithm.
I want to make such an algorithm:
The project has a touch button and a regular one.
When the button is pressed, the function should change.
Then if you press the touch button, then the algorithm should do the function inside the cell that it stopped.

For example:
1 cell:
Serial.println("1");
2 cell:
Serial.println("2");
3 cell:
Serial.println("3");
4 cell:
Serial.println("4");

If you press the normal button, it will switch to the second cell. Then press the touch button to perform the function of the second cell. etc

Button Code :

void setup() {
  Serial.begin(9600);
  pinMode(18, INPUT_PULLUP); //button
  pinMode(5, INPUT); //touch button
}

void loop() {
  if (digitalRead(18) == 0) {
    Serial.println("0");
  }
}

Why don't try to write code yourself, using the tutorial

Of course I tried. But I could not make the algorithm that I need

void setup() {
  Serial.begin(9600);
  pinMode(18, INPUT_PULLUP); //button
  pinMode(5, INPUT); //touch button
}

void loop() {
  switch (digitalRead(18) == 0) {
    case 1:
      if (digitalRead(5) == 1) {
        Serial.println("1");
      }
      break;
    case 2:
      if (digitalRead(5) == 1) {
        Serial.println("2");
      }
      break;
    default:
      // if nothing else matches, do the default
      // default is optional
      break;
  }
}

Result of digitalRead() == 0 condition can be true (1) or false (0). So your case 2 branch will never be executed.

But if I do so, the function will not switch

switch (digitalRead(18)) {
    case 1:
      if (digitalRead(5) == 1) {
        Serial.println("1");
      }
      break;
    case 2:
      if (digitalRead(5) == 1) {
        Serial.println("2");
      }
      break;
    default:
      break;
  }

You have skipped some important lessons, I think.

You need to learn how to increase a counter when a button is pressed. Only then can you make your switch-case statement.

Increasing a counter when a button is pressed is not as easy as it sounds. You have to learn about state-change detection and button debouncing.

Again, the digitalRead() function returns either 0 or 1, but not 2. Your second branch will never be reached.
I think you don't need switch statement at all. Your problem can resolved with combination of if statement and incrementing counter.

I think we should not encourage @muhammedjan to get into "bad habits".

According to the Arduino documentation for digitalRead();

Returns
HIGH or LOW

It doesn't say 0 or 1. We know from experience that 0 and 1 are equivalent, and that has never changed and probably never will change. But assumptions like that are the start of a slippery slope!

It would be "ok" to put

switch (digitalRead(18)) {
    case LOW:
     
    case HIGH:

but you might was well use if() instead of switch().

(Sorry about this, I seem to have woken up with my purist's hat on today!)

Please do not start again :slight_smile:
I have participated yesterday in that thread about the matter...

Do you agree with me that the result of a digitalRead cannot be two in the current conditions? :slight_smile: It's enough

Yes, of course I agree about that :slight_smile:

Ah, this one:

85 posts! Clearly a subject that many forum members feel passionate about.

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