Nimb
July 5, 2020, 12:15am
1
Hi guys, can i make this more clean?
im trying the counter reach in 3 when a specific order of switch on (like 2nd, 1st, 3rd).
this switch
this only to u know how is wired
this i figured out, but if u can help me, i appreciate
int portInt[] = {9, 10, 11, 12, 13, 24};
int led5[]= {53, 51, 49};
void setup() {
for (int l5 = 0; l5 <= 3; l5++) {
pinMode(led5[l5], OUTPUT);
}
for (int pI = 0; pI <= 5; pI++) {
pinMode(portInt[pI], INPUT);
}
Serial.begin(9600);
}
void loop() {
interruptores();
}
void RGB_5(int r5_value, int g5_value, int b5_value)
{
analogWrite(led5[0], r5_value);
analogWrite(led5[1], g5_value);
analogWrite(led5[2], b5_value);
}
void interruptores()
{
static int countInt = 0; // variable for reading the pushbutton status
Serial.println(countInt);
if (countInt == 3) {
digitalWrite(led5[0], HIGH);
} else {
digitalWrite(led5[0], LOW);
}
if ((digitalRead(portInt[4]) == LOW) && (digitalRead(portInt[2]) == LOW) && (digitalRead(portInt[0]) == LOW)) {
countInt = 0;
}
if ((digitalRead(portInt[4]) == HIGH) && (digitalRead(portInt[0]) == LOW)) {
countInt--;
}
if ((digitalRead(portInt[0]) == HIGH) && (digitalRead(portInt[2]) == LOW)) {
countInt--;
}
if ((digitalRead(portInt[2]) == HIGH) && (countInt == 0)) {
countInt++;
}
if ((digitalRead(portInt[0]) == HIGH) && (countInt == 1)) {
countInt++;
}
if ((digitalRead(portInt[4]) == HIGH) && (countInt == 2)) {
countInt++;
}
if ((digitalRead(portInt[1]) == HIGH) || (digitalRead(portInt[3]) == HIGH) || (digitalRead(portInt[5]) == HIGH)) {
countInt--;
}
}
blh64
July 5, 2020, 6:40pm
2
Look at the State Change Detection example in the IDE (File->examples->02.Digital->State Change Detection). to figure out how to detect when a switch changes state. This change of state is when you want to react, not keep increasing/decreasing you counter simply because a switch is HIGH or LOW.
gcjr
July 5, 2020, 7:52pm
3
is this of any help (had only 3 push-buttons)
#if 0
int portInt[] = {9, 10, 11, 12, 13, 24};
int led5[]= {53, 51, 49};
#else
#define ON LOW
#define OFF HIGH
byte portInt [] = { A1, A2, A3 };
byte led5 [] = { 10, 11, 12};
#endif
// -----------------------------------------------------------------------------
void setup () {
for (int i = 0; i < sizeof (led5); i++) {
digitalWrite (led5 [i], OFF);
pinMode (led5 [i], OUTPUT);
}
for (int i = 0; i < sizeof(portInt); i++) {
pinMode (portInt [i], INPUT);
}
Serial.begin (9600);
}
// -----------------------------------------------------------------------------
void loop () {
interruptores ();
}
// -----------------------------------------------------------------------------
void RGB_5 (int r5_value, int g5_value, int b5_value)
{
analogWrite (led5[0], r5_value);
analogWrite (led5[1], g5_value);
analogWrite (led5[2], b5_value);
}
// -----------------------------------------------------------------------------
void interruptores ()
{
static int countLst = 0;
static int countInt = 0; // variable for reading the pushbutton status
if (countLst != countInt) {
countLst = countInt;
Serial.println (countInt);
}
byte bits = 0;
for (int i = 0; i < sizeof(portInt); i++) {
bits |= (digitalRead (portInt[i]) == ON) << i;
}
if (0 == bits)
countInt = 0;
switch (countInt) {
case 0:
if (0x02 == bits)
countInt ++;
break;
case 1:
if (0x06 == bits)
countInt ++;
break;
case 2:
if (0x07 == bits)
countInt ++;
break;
}
#if 0
char s [20];
sprintf (s, " bits %02x, count %d", bits, countInt);
Serial.println (s);
#endif
digitalWrite (led5[0], 3 == countInt ? ON : OFF);
}