South Node Code II. Arrays mixed up.

There’s a lot of fluff and inefficiency. THAT, I know. I’m trying to nail it down working, then optimize.

It cycles through an array, with rows being input pins, it then is to read each and store that value in now. Then it is to compare it to was. That part is broken. Was is prescient. As in, it sees the future - or simply the next iteration of code. Why if I set was to now further DOWN than my comparison, is my comparison always 1:1? Except, in the start, when was is zero;d out?
To debug some stuff is commented out, and you’ll see that I have some “echos” to serial and a IF statement logically inside out.
Worse, the output of now[10] is almost erratic, even though it’s hooked to a pir sensor between my wrists with a long output span. I run my InputTest - the inputs are stable. I’m vexed.
```
*byte sdbr = 3; // South Door Bolt Reed
byte sdr = 4; // South Door Open Reed

byte smg = A1; // South Magnet Green (1.46V. voltage divider)
byte smo = A2; // South Magnet On (4.23V via VD)
byte spk = A3; // Speaker
byte smt = 6; // South Magnet Trigger / Unlock
byte smpex = 7; // South Magnet PEX NC
byte smc = 8; // South Magnet Contact
byte swr = 9; // South Window Reed
//byte kp = 10; // Kitchen PIR
byte g4r = 11; // G4 Reed
//byte str = 12; // Strobe Light IF
byte g4 = A0; // Garage 4 Clicker Button IF

char* pinNames[12] = { “4” ,    “3”,  “A2”, “A1”,  “7”,  “8” , “9”, “10”, “11” };
char* ssNames[12] = { “sdr”, “sdbr”, “smo”, “smg”, “smpex”, “smc”, “swr”, “kp”, “g4r” };
char* ssNamesLong[12] = { “South Door Reed”, “South Door Deadbolt Reed”, “South Maglock Voltage”, “South Maglock Green Light 1.46V”, “South Magnet PEX N/C”, “South Maglock Contact”, “South Window Reed”, “Kitchen PIR”, “Garage 4 Reed” };

int was[14];
long ms[14];
int now[14];
int i;

void g4button() {
  /*
    Rule:
    IF the light is green and a reed on window is hit, trigger.
  */
  digitalWrite(g4, HIGH);
  delay(100);
  digitalWrite(g4, LOW);
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(A0, OUTPUT);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT);
  pinMode(6, OUTPUT);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  for (i = 0; i < 12; i++) {
    // Array init loop
    was[i] = 0;
    now[i] = 0;
    ms[i] = millis();
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(13, LOW);
  noTone(A3);
  // Detect state changes
  for (i = 0; i < 12; i++) {
    char* sName = ssNames[i];

if (isAlpha(pinNames[i]) == true) { // Then read in Analog value
      int readVal = analogRead(ssNames[i]);
      now[i] = 0;
      if (290 < readVal < 310) now[i] = 1; // HIGH if in target ranges
      if (855 < readVal < 875) now[i] = 1;
    }
    else now[i] = digitalRead(sName); // Set current state
    String strT = String(was[i]) + " / " + String(now[i]);
  if (pinNames[i] = 10) Serial.println(strT);
    delay(100);
    if (now[i] == was[i]) int l = 1;  // ** Change in state ? **
else {
        ms[i] = millis; // Set the time
        //int way = (int(now[i]) * 2) + (int(was[i]) * 1); // 0 Lo>Lo, 1 Hi>lo, 2 Lo>Hi, 3 Hi>Hi
        //if (way == 1) Serial.println("Change! " + String(ssNames[i]) + String(way) + long(ms[i] - millis()));
        //if (way == 2) Serial.println("Change! " + String(ssNames[i]) + String(way) + long(ms[i] - millis()));
        digitalWrite(13, HIGH);
        tone(A3, 80 + i * 60);
        // Set new states
        int dur = millis - ms[i];
        delay(100);
        was[i] = now[i];
        //Serial.println(String(pinNames[i]) + ’ ’ + String(ssNames[i]) + ’ ’ + String(ssNamesLong[i]) + ’ ’ + String(now[i]));
      }
  }
}

_```*_

if (290 < readVal < 310)You cannot test for a range of values like that in C

Test whether readVal is greater than 290 AND whether readVal is less than 310

    if (now[i] == was[i])
      int l = 1;   // ** Change in state ? **

This declares a NEW local variable called ‘l’, sets it to 1, then throws it away. Probably not what you want.

TURN UP YOUR WARNING LEVEL. This will point out suspicious programming.

/Users/john/Downloads/proj1_487/proj1_487.ino:17:83: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
 char* pinNames[12] = { "4" ,    "3",   "A2", "A1",   "7",   "8" , "9", "10", "11" };
                                                                                   ^
/Users/john/Downloads/proj1_487/proj1_487.ino:17:83: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:17:83: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:17:83: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:17:83: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:17:83: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:17:83: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:17:83: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:17:83: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:18:87: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
 char* ssNames[12] = { "sdr", "sdbr", "smo", "smg", "smpex", "smc", "swr", "kp", "g4r" };
                                                                                       ^
/Users/john/Downloads/proj1_487/proj1_487.ino:18:87: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:18:87: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:18:87: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:18:87: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:18:87: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:18:87: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:18:87: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:18:87: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:19:235: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
 char* ssNamesLong[12] = { "South Door Reed", "South Door Deadbolt Reed", "South Maglock Voltage", "South Maglock Green Light 1.46V", "South Magnet PEX N/C", "South Maglock Contact", "South Window Reed", "Kitchen PIR", "Garage 4 Reed" };
                                                                                                                                                                                                                                           ^
/Users/john/Downloads/proj1_487/proj1_487.ino:19:235: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:19:235: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:19:235: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:19:235: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:19:235: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:19:235: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:19:235: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino:19:235: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
/Users/john/Downloads/proj1_487/proj1_487.ino: In function 'void loop()':
/Users/john/Downloads/proj1_487/proj1_487.ino:69:27: warning: invalid conversion from 'char*' to 'int' [-fpermissive]
     if (isAlpha(pinNames[i]) == true) { // Then read in Analog value
                           ^
In file included from /Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.20/cores/arduino/Arduino.h:230:0,
                 from sketch/proj1_487.ino.cpp:1:
/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.20/cores/arduino/WCharacter.h:54:16: note: initializing argument 1 of 'boolean isAlpha(int)'
 inline boolean isAlpha(int c)
                ^
/Users/john/Downloads/proj1_487/proj1_487.ino:70:41: warning: invalid conversion from 'char*' to 'uint8_t {aka unsigned char}' [-fpermissive]
       int readVal = analogRead(ssNames[i]);
                                         ^
In file included from sketch/proj1_487.ino.cpp:1:0:
/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.20/cores/arduino/Arduino.h:136:5: note: initializing argument 1 of 'int analogRead(uint8_t)'
 int analogRead(uint8_t);
     ^
/Users/john/Downloads/proj1_487/proj1_487.ino:72:15: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
       if (290 < readVal < 310) now[i] = 1; // HIGH if in target ranges
               ^
/Users/john/Downloads/proj1_487/proj1_487.ino:73:15: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
       if (855 < readVal < 875) now[i] = 1;
               ^
/Users/john/Downloads/proj1_487/proj1_487.ino:75:36: warning: invalid conversion from 'char*' to 'uint8_t {aka unsigned char}' [-fpermissive]
     else now[i] = digitalRead(sName); // Set current state
                                    ^
In file included from sketch/proj1_487.ino.cpp:1:0:
/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.20/cores/arduino/Arduino.h:135:5: note: initializing argument 1 of 'int digitalRead(uint8_t)'
 int digitalRead(uint8_t);
     ^
/Users/john/Downloads/proj1_487/proj1_487.ino:77:21: warning: invalid conversion from 'int' to 'char*' [-fpermissive]
     if (pinNames[i] = 10) Serial.println(strT);
                     ^
/Users/john/Downloads/proj1_487/proj1_487.ino:77:25: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
     if (pinNames[i] = 10) Serial.println(strT);
                         ^
/Users/john/Downloads/proj1_487/proj1_487.ino:80:11: warning: unused variable 'l' [-Wunused-variable]
       int l = 1;   // ** Change in state ? **
           ^
/Users/john/Downloads/proj1_487/proj1_487.ino:82:13: warning: invalid conversion from 'long unsigned int (*)()' to 'long int' [-fpermissive]
       ms[i] = millis; // Set the time
             ^
/Users/john/Downloads/proj1_487/proj1_487.ino:89:30: warning: pointer to a function used in arithmetic [-Wpointer-arith]
       int dur = millis - ms[i];
                              ^
/Users/john/Downloads/proj1_487/proj1_487.ino:89:24: warning: invalid conversion from 'long unsigned int (*)()' to 'int' [-fpermissive]
       int dur = millis - ms[i];
                        ^
/Users/john/Downloads/proj1_487/proj1_487.ino:89:11: warning: unused variable 'dur' [-Wunused-variable]
       int dur = millis - ms[i];
           ^
Sketch uses 6106 bytes (18%) of program storage space. Maximum is 32256 bytes.
Global variables use 445 bytes (21%) of dynamic memory, leaving 1603 bytes for local variables. Maximum is 2048 bytes.

Looks like you left the ‘()’ off of millis() several places. That uses the address of the millis() function rather then calling the function.

In several places you are using ‘=’ where you intend to use ‘==’ to compare pinNames (a character pointer) to an integer constant. For example "if (pinNames = 10) Serial.println(strT);" That won’t work. You should use strcmp(pinNames[i], "10") == 0 to determine if the pin is named “10”. Better might be to use the integer names:
*_ <em>*const byte pinNames[12] = {4,  3,  A2, A1,  7, 8 , 9, 10, 11 };*</em> _*
Then you can use: if (pinNames[i] == 10) Serial.println(strT); (Note the ‘==’ in place of ‘=’.)

I forgot the ()'s. However I am aware of the == vs = since I transitioned into VBA a while back, as VBA didn’t give a crap and I felt that odd. The bit where I declare a var in loop is a debug done in angry haste. Problem is, was always equals now at middle. It should change once i changes, and also once now changes, but only always equal out at bottom, where they are set to match. I am ONLY setting was*, not was.*

Can digitalRead be used with anything but a literal name of the pin? It can't even function with a variable set to pin name? What good is that? I give up on this sh!t.

mattlogue:
Can digitalRead be used with anything but a literal name of the pin? It can't even function with a variable set to pin name? What good is that? I give up on this sh!t.

const int dontBeADickHead = 4;
digitalWrite(dontBeADickHead, HIGH);

Hold on… the tone of your answer swayyed me from point. I used this. I’m pissed over the fact you can’t use a variable in anything. That is fault of compiler, 90%.

  for (i = 0; i < loopMax; i++) {
    switch (i) {
      case 0: {
          now[i] = digitalRead(0);
          break;
        }
      case 1: {
          now[i] = digitalRead(1);
          break;
        }
      case 2: {
          now[i] = digitalRead(2);
          break;
        }

      case 3: {
          now[i] = digitalRead(3);
          break;
        }

      case 4: {
          now[i] = digitalRead(4);
          break;
        }

      case 5: {
          now[i] = digitalRead(5);
          break;
        }

      case 6: {
          now[i] = digitalRead(6);
          break;
        }

      case 7: {
          now[i] = digitalRead(7);
          break;
        }

      case 8: {
          now[i] = digitalRead(8);
          break;
        }

      case 9: {
          now[i] = digitalRead(9);
          break;
        }

      case 10: {
          now[i] = digitalRead(10);
          break;
        }

      case 11: {
          now[i] = digitalRead(11);
          break;
        }

      case 12: {
          now[i] = digitalRead(12);
          break;
        }

      case 13: {
          now[i] = digitalRead(13);
          break;
        }

      case 14: {
          now[i] = digitalRead(A0);
          awas[i - 14] = analogRead(A0);
          break;
        }

      case 15: {
          now[i] = digitalRead(A1);
          awas[i - 14] = analogRead(A1);
          break;
        }

      case 16: {
          now[i] = digitalRead(A2);
          awas[i - 14] = analogRead(A2);
          break;
        }
      case 17: {
          now[i] = digitalRead(A3);
          awas[i - 14] = analogRead(A3);
          break;
        }

      case 18: {
          now[i] = digitalRead(A4);
          awas[i - 14] = analogRead(A4);
          break;
        }

      case 19: {
          now[i] = digitalRead(A5);
          awas[i - 14] = analogRead(A5);
          break;
        }
      case 19: {
          now[i] = digitalRead(A6);
          awas[i - 14] = analogRead(A6);
          break;
        }
      case 19: {
          now[i] = digitalRead(A7);
          awas[i - 14] = analogRead(A7);
          break;
        }
    }

Long and inefficient but it’s a nice workaround and I suppose I could hide it in an include.

for (int i = 0; i < loopMax; i++)
{
  now[i] = digitalRead(i);
}

mattlogue:
That is fault of compiler, 90%.

It's a poor craftsman who blames his tools.

gfvalvo:
It's a poor craftsman who blames his tools.

As a new kid on the block I find such remark unecessry.

Even after years of computing I have found it is quite often better to write a "long code" BEFORE in can be concatenated to single or few functions.
Definitely easy to debug or just verify the code flow.

232:
As a new kid on the block I find such remark unecessry.

My comment had absolutely nothing to do with the code posted, but everything to do with the attitude displayed by the OP.

I give up on this sh!t.

I'm pissed over the fact you can't use a variable in anything.

That is fault of compiler, 90%.

Again - as a new kid on the block I have to ask - is that customary to pass judgment on this forum or is this forum primarily for technical issues ?
Like to avoid any (future) conflicts.

232:
Again - as a new kid on the block I have to ask - is that customary to pass judgment on this forum or is this forum primarily for technical issues ?
Like to avoid any (future) conflicts.

It’s like 95% dogma, 5% really valuable assistance.

UKHeliBob:

for (int i = 0; i < loopMax; i++)

{
  now[i] = digitalRead(i);
}

How would i be declared (other than a jackwagon?)? As a constant? I am hearing digitalRead likes only constants between it’s parenthesis?

I declare variables like i as byte, and do it before setup, so it is global. Then I use it all over the place.
byte gives you 0 to 255, covers all the pins on 328P, 1284P, and 2560 boards.
It can't be a constant and then used as a counter/index in a for loop.

mattlogue:
How would i be declared (other than a jackwagon?)? As a constant? I am hearing digitalRead likes only constants between it's parenthesis?

i is declared in the for loop initialisation, as in the code snippet. How could it ever be a constant when its value varies ?

Where did you hear the rubbish that you are spouting ?