Skip to main content
Became Hot Network Question
added 222 characters in body
Source Link
Michel Keijzers
  • 13k
  • 7
  • 42
  • 59

*/

// constants won't change. They're used here to set pin numbers: char buttonChar1 = 'a'; // the number of the pushbutton pin const int buttonPin1 = 2; const int buttonPin2 = 3; const int buttonPin3 = 4; const int buttonPin4 = 5; const int buttonPin5 = 6; const int ledPin = 13; // the number of the LED pin

// variables will change: int buttonState1 = 0; // variable for reading the pushbutton status int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; int pinsArray[5] = {2, 3, 4, 5, 6}; void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); }

void loop() { // read the state of the pushbutton value: buttonState1 = digitalRead(buttonPin1) == 2; buttonState2 = digitalRead(buttonPin2) == 3; buttonState3 = digitalRead(buttonPin3) == 4; buttonState4 = digitalRead(buttonPin4) == 5; buttonState5 = digitalRead(buttonPin5) == 6; // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

switch (pinsArray[5]) { case 2: // digitalWrite(ledPin, HIGH); Serial.println("button1"); break; case 3: // digitalWrite(ledPin, HIGH); Serial.println("dim"); break; case 4: // Serial.println("medium"); break; case 5: // Serial.println("bright"); break; } }

*/

// constants won't change. They're used here to set pin numbers:
char buttonChar1 = 'a';     // the number of the pushbutton pin
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int pinsArray[5] = {2, 3, 4, 5, 6};
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1) == 2;
  buttonState2 = digitalRead(buttonPin2) == 3;
  buttonState3 = digitalRead(buttonPin3) == 4;
  buttonState4 = digitalRead(buttonPin4) == 5;
  buttonState5 = digitalRead(buttonPin5) == 6;
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

  switch (pinsArray[5]) {
    case 2:    // 
      digitalWrite(ledPin, HIGH);
      Serial.println("button1");
      break;
    case 3:     // 
      digitalWrite(ledPin, HIGH);
      Serial.println("dim");
      break;
    case 4:    // 
      Serial.println("medium");
      break;
    case 5:    //
      Serial.println("bright");
      break;
  }
  }

*/

// constants won't change. They're used here to set pin numbers: char buttonChar1 = 'a'; // the number of the pushbutton pin const int buttonPin1 = 2; const int buttonPin2 = 3; const int buttonPin3 = 4; const int buttonPin4 = 5; const int buttonPin5 = 6; const int ledPin = 13; // the number of the LED pin

// variables will change: int buttonState1 = 0; // variable for reading the pushbutton status int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; int pinsArray[5] = {2, 3, 4, 5, 6}; void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); }

void loop() { // read the state of the pushbutton value: buttonState1 = digitalRead(buttonPin1) == 2; buttonState2 = digitalRead(buttonPin2) == 3; buttonState3 = digitalRead(buttonPin3) == 4; buttonState4 = digitalRead(buttonPin4) == 5; buttonState5 = digitalRead(buttonPin5) == 6; // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

switch (pinsArray[5]) { case 2: // digitalWrite(ledPin, HIGH); Serial.println("button1"); break; case 3: // digitalWrite(ledPin, HIGH); Serial.println("dim"); break; case 4: // Serial.println("medium"); break; case 5: // Serial.println("bright"); break; } }

*/

// constants won't change. They're used here to set pin numbers:
char buttonChar1 = 'a';     // the number of the pushbutton pin
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int pinsArray[5] = {2, 3, 4, 5, 6};
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1) == 2;
  buttonState2 = digitalRead(buttonPin2) == 3;
  buttonState3 = digitalRead(buttonPin3) == 4;
  buttonState4 = digitalRead(buttonPin4) == 5;
  buttonState5 = digitalRead(buttonPin5) == 6;
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

  switch (pinsArray[5]) {
    case 2:    // 
      digitalWrite(ledPin, HIGH);
      Serial.println("button1");
      break;
    case 3:     // 
      digitalWrite(ledPin, HIGH);
      Serial.println("dim");
      break;
    case 4:    // 
      Serial.println("medium");
      break;
    case 5:    //
      Serial.println("bright");
      break;
  }
  }
Source Link
Au42
  • 19
  • 2

using a switch case with a 5-position switch and serial output

I'm trying to get started on developing a menu system for my LCD1602. I have a Parallax 5-position switch (up,down,left,right,center) and I would like to interface the two.

I've been trying to get some code working but I'm kind of lost. I don't have a lot of recent experience with Arduino and could use some advice. I realize that there is a version of the 1602 that comes with a keypad integrated into the circuit but I don't have that model. I have a plain-jane screen and a separate 5-position switch.

I'm also aware that the easiest way to accomplish what I desire is with a resistor network and mapping the ADC values. However, I don't want to make an analog switch, I want to make a digital switch.

Is there any way I can take the pins and put them into an array and then translate that into a switch case statement?

I've tried several different ideas and nothing seems to be working. Forgive the example code, it's hacked together.

below is my code

*/

// constants won't change. They're used here to set pin numbers: char buttonChar1 = 'a'; // the number of the pushbutton pin const int buttonPin1 = 2; const int buttonPin2 = 3; const int buttonPin3 = 4; const int buttonPin4 = 5; const int buttonPin5 = 6; const int ledPin = 13; // the number of the LED pin

// variables will change: int buttonState1 = 0; // variable for reading the pushbutton status int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; int pinsArray[5] = {2, 3, 4, 5, 6}; void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); }

void loop() { // read the state of the pushbutton value: buttonState1 = digitalRead(buttonPin1) == 2; buttonState2 = digitalRead(buttonPin2) == 3; buttonState3 = digitalRead(buttonPin3) == 4; buttonState4 = digitalRead(buttonPin4) == 5; buttonState5 = digitalRead(buttonPin5) == 6; // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

switch (pinsArray[5]) { case 2: // digitalWrite(ledPin, HIGH); Serial.println("button1"); break; case 3: // digitalWrite(ledPin, HIGH); Serial.println("dim"); break; case 4: // Serial.println("medium"); break; case 5: // Serial.println("bright"); break; } }