[HOW] PRINT TEXT IN SERIAL MONITOR USING 3 SLIDESWITCH ???

Hello, i’m kind of beginner here so don’t judge me :D.

So go to the point, I have 3 slide switches and 3 leds. My task is to push 1 or 2 or 3 slide switch and in serial motor it should write first if you press second it would say second and third. The thing is you only one of 3 slideswitch you can press, if you press one more ( for example 1 and 3) it should leave to default data which is 1 and 0.

I know that I have to use ||(OR) function but i can separate for one slideswitch data.

It is my code: ( i do not post all code i think it is unnessasary i guess :D)

Serial.println(slide1);
Serial.println(slide2);
Serial.println(slide3);

if ( slide1 == 0 || slide2 == 0 || slide3 == 0){

}

So what i basically want to say slide1, 2,3 are my data my slideswitches. and if i write

Serial.println(slide1);
Serial.print(" First");

it basically then you push third or second button and it also writes “First”

i ask someone help just how to print for only slide1 or only for slide 2.

I think you understand my problem. :slight_smile: Have a nice evening :slight_smile:

You do something like this as an example of one switch:

byte slide1 ;
. . .

pinMode( slide1 , INPUT ) ;  // or INPUT_PULLUP

. . . 

if ( digitalRead( slide1 ) == HIGH  ) {   // or LOW depending on how the switch is wired
   // your logic here

}

Your diagram is difficult to follow, but it does show that you are missing current limiting resistors for the LEDs.
If you have any more code, post it.

dovysh9999:
It is my code: ( i do not post all code i think it is unnessasary i guess :D)

Post the full program. The problem is almost always in the other part - that's why have not solved it yourself :slight_smile:

...R

here you go: but your method didn’t helped I guess because you wouldn’t have full code.

void setup()
{
pinMode(7, INPUT);
pinMode(6, INPUT);
pinMode(5, INPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
Serial.begin(9600);
}

void loop()
{
int slide1 = digitalRead(7);
int slide2 = digitalRead(6);
int slide3 = digitalRead(5);
Serial.println(slide1);
Serial.println(slide2);
Serial.println(slide3);
if (! slide1 == 1 && slide2 == 0 && slide3 == 0)
{
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}
else {
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
}
if (! slide1 == 0 && slide2 == 0 && slide3 == 0)
{
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
delay(500);
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
delay(500);
}
if (! slide2 == 0 && slide1 == 0 && slide3 == 0)
{
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
delay(500);
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
delay(500);
}
if (! slide3 == 0 && slide2 == 0 && slide1 == 0)
{
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
delay(500);
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
delay(500);
}
if ( slide1 == 0 || slide2 == 0 || slide3 == 0)
{
}
if ( slide1 == 1 && slide2 == 1 && slide3 == 1){

}
}

// I attached also scheme how look like all thing

If you use the code button </> it posts your code like this and makes it much easier to read and to copy to a text editor

void setup()
{
    pinMode(7, INPUT);
    pinMode(6, INPUT);
    pinMode(5, INPUT);
    pinMode(A0, OUTPUT);
    pinMode(A1, OUTPUT);
    pinMode(A2, OUTPUT);
    Serial.begin(9600);
}

void loop()
{
    int slide1 = digitalRead(7);
    int slide2 = digitalRead(6);
    int slide3 = digitalRead(5);
    Serial.println(slide1);
    Serial.println(slide2);
    Serial.println(slide3);
    if (! slide1 == 1 && slide2 == 0 && slide3 == 0)
    {
        digitalWrite(A0, HIGH);
        digitalWrite(A1, HIGH);
        digitalWrite(A2, HIGH);
    }
    else {
        digitalWrite(A0, LOW);
        digitalWrite(A1, LOW);
        digitalWrite(A2, LOW);
    }
    if (! slide1 == 0 && slide2 == 0 && slide3 == 0)
    {
        digitalWrite(A0, HIGH);
        digitalWrite(A1, HIGH);
        digitalWrite(A2, HIGH);
        delay(500);
        digitalWrite(A0, LOW);
        digitalWrite(A1, LOW);
        digitalWrite(A2, LOW);
        delay(500);
    }
    if (! slide2 == 0 && slide1 == 0 && slide3 == 0)
    {
        digitalWrite(A0, HIGH);
        digitalWrite(A1, HIGH);
        digitalWrite(A2, HIGH);
        delay(500);
        digitalWrite(A0, LOW);
        digitalWrite(A1, LOW);
        digitalWrite(A2, LOW);
        delay(500);
    }
    if (! slide3 == 0 && slide2 == 0 && slide1 == 0)
    {
        digitalWrite(A0, HIGH);
        digitalWrite(A1, HIGH);
        digitalWrite(A2, HIGH);
        delay(500);
        digitalWrite(A0, LOW);
        digitalWrite(A1, LOW);
        digitalWrite(A2, LOW);
        delay(500);
    }
    if ( slide1 == 0 || slide2 == 0 || slide3 == 0)
    {
    }
    if ( slide1 == 1 && slide2 == 1 && slide3 == 1){

    }
}

This piece of code

    int slide1 = digitalRead(7);
    int slide2 = digitalRead(6);
    int slide3 = digitalRead(5);
    Serial.println(slide1);
    Serial.println(slide2);
    Serial.println(slide3);

should print something like
1
0
1
depending on the states of the switches. But it will probably print them far too fast to be useful. You could add some code so the values are only printed if one of them changes. It would probably also be easier to see if you print the 3 values on one line by using Serial.print() for the first 2 values.

This piece of code

    if (! slide1 == 1 && slide2 == 0 && slide3 == 0)
    {
        digitalWrite(A0, HIGH);
        digitalWrite(A1, HIGH);
        digitalWrite(A2, HIGH);
    }
    else {
        digitalWrite(A0, LOW);
        digitalWrite(A1, LOW);
        digitalWrite(A2, LOW);
    }

may not do what you want because, with 3 tests there are 8 possibilities and any of 7 of them can trigger the ELSE part.

Cascading the IF statements can make the logic easier to create and debug - for example

if (slide1 == 0) {
  if (slide2 == 0) {
  }
  else {  // slide2 == 1
  }
else { // slide1 == 1 and we don't know what the others are yet
}

…R

Where are the current limiting resistors in your schematic?

One way is to use a variable to remember which switch was activated first.

void loop()
{
  // variable to remember active switch
  // 0 indicates no switch is active
  static byte activeSwitch = 0;

  ...
  ...
}

Next you can read the switches and set the variable according to the selected switch

void loop()
{
  // variable to remember active switch
  // 0 indicates no switch is active
  static byte activeSwitch = 0;

  // if no switch was active before
  // read the three switches and if one is active set the activeSwitch variable to indicate which one
  if (activeSwitch == 0)
  {
    if (digitalRead(7) == HIGH)
    {
      activeSwitch = 1;
    }
    else if (digitalRead(6) == HIGH)
    {
      activeSwitch = 2;
    }
    else if (digitalRead(5) == HIGH)
    {
      activeSwitch = 3;
    }
    else
    {
      activeSwitch = 0;
    }
  }
  ...
  ...
}

So now you know if a switch was active and if one was active, you know which one.

And lastly you can take action based on the value of activeSwitch

void loop()
{
  // variable to remember active switch
  // 0 indicates no switch is active
  static byte activeSwitch = 0;

  // if no switch was active before
  // read the three switches and if one is active set the activeSwitch variable to indicate which one
  if (activeSwitch == 0)
  {
    if (digitalRead(7) == HIGH)
    {
      activeSwitch = 1;
    }
    else if (digitalRead(6) == HIGH)
    {
      activeSwitch = 2;
    }
    else if (digitalRead(5) == HIGH)
    {
      activeSwitch = 3;
    }
    else
    {
      activeSwitch = 0;
    }
  }

  // take action based on active switch
  switch(activeSwitch)
  {
    case 0:
      // do whatever if no switch was active
      break;
    case 1
      // do whatever if switch 1 is active
      break;
    case 2:
      // do whatever if switch 2 is active
      break;
    case 3:
      // do whatever if switch 3 is active
      break;
  }
  
}

Advise: get rid of the hard-code pin numbers.

since you are defining a single state using three buttons you may want to simply put all of their values into a single variable:

void setup()
{
  pinMode(7, INPUT);
  pinMode(6, INPUT);
  pinMode(5, INPUT);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  Serial.begin(9600);
}

void loop() 
{
  static byte lastSwitchState = 0b0;
  byte switchState = 0b0;
  switchState = (digitalRead(7) & digitalRead(6) << 1 & digitalRead(5) << 2);
  if(switchState != lastSwitchState)
  {
    digitalWrite(A0, switchState & 0b001); // 7
    digitalWrite(A1, switchState & 0b010); // 6
    digitalWrite(A2, switchState & 0b100); // 5
    lastSwitchState = switchState;
  }
  delay(100); // crude debounce
}

using bit masks

That may be pushing OP a bit far, but completely worthwhile in the long term!