weniger Speicherplatz verbrauchen? (Deklarationen)

Hallo Leute,

Ich hätte da mal eine Frage:

Wie kann ich dieses Beispiel,

int touchSensor1 = 1;
int touchSensor2 = 2;
int touchSensor3 = 3;
int touchSensor4 = 4;
int touchSensor5 = 5;
int touchSensor6 = 6;
int touchSensor7 = 7;
int touchSensor8 = 8;
int touchSensor9 = 9;
int touchSensor10 = 10;
int touchSensor11 = 11;
int touchSensor12 = 12;
int touchSensor13 = 13;
int touchSensor14 = 14;
int touchSensor15 = 15;
int touchSensor16 = 16;
int touchSensor17 = 17;
int touchSensor18 = 18;
int touchSensor19 = 19;
int touchSensor20 = 20;
int touchSensor21 = 21;
int touchSensor22 = 22;
int touchSensor23 = 23;
int touchSensor24 = 24;
int touchSensor25 = 25;
int touchSensor26 = 26;
int touchSensor27 = 27;
int touchSensor28 = 28;
int touchSensor29 = 29;
int touchSensor30 = 30;
int touchSensor31 = 31;
int touchSensor32 = 32;
int touchSensor33 = 33;
int touchSensor34 = 34;
int touchSensor35 = 35;
int touchSensor36 = 36;
int touchSensor37 = 37;
int touchSensor38 = 38;
int touchSensor39 = 39;
int touchSensor40 = 40;
int touchSensor41 = 41;
int touchSensor42 = 42;
int touchSensor43 = 43;
int touchSensor44 = 44;
int touchSensor45 = 45;
int touchSensor46 = 46;
int touchSensor47 = 47;
int touchSensor48 = 48;
int touchSensor49 = 49;
int touchSensor50 = 50;
int touchSensor51 = 51;
int touchSensor52 = 52;
int touchSensor53 = 53;
int touchSensor54 = 54;
int touchSensor55 = 55;
int touchSensor56 = 56;
int touchSensor57 = 57;
int touchSensor58 = 58;
int touchSensor59 = 59;
int touchSensor60 = 60;
int touchSensor61 = 61;
int touchSensor62 = 62;
int touchSensor63 = 63;
int touchSensor64 = 64;
int touchSensor65 = 65;
int touchSensor66 = 66;
int touchSensor67 = 67;
int touchSensor68 = 68;
int touchSensor69 = 69;
int touchSensor70 = 70;
int touchSensor71 = 71;
int touchSensor72 = 72;
int touchSensor73 = 73;
int touchSensor74 = 74;
int touchSensor75 = 75;
int touchSensor76 = 76;
int touchSensor77 = 77;
int touchSensor78 = 78;
int touchSensor79 = 79;
int touchSensor80 = 80;
int touchSensor81 = 81;
int touchSensor82 = 82;
int touchSensor83 = 83;
int touchSensor84 = 84;
int touchSensor85 = 85;
int touchSensor86 = 86;
int touchSensor87 = 87;
int touchSensor88 = 88;
int touchSensor89 = 89;
int touchSensor90 = 90;
int touchSensor91 = 91;
int touchSensor92 = 92;
int touchSensor93 = 93;
int touchSensor94 = 94;
int touchSensor95 = 95;
int touchSensor96 = 96;
int touchSensor97 = 97;
int touchSensor98 = 98;
int touchSensor99 = 99;
int touchSensor100 = 100;

... oder dieses hier,

int buttonState1 = CS.digitalRead(touchSensor1);   //1
  int buttonState2 = CS.digitalRead(touchSensor2);
  int buttonState3 = CS.digitalRead(touchSensor3);
  int buttonState4 = CS.digitalRead(touchSensor4);
  int buttonState5 = CS.digitalRead(touchSensor5);
  int buttonState6 = CS.digitalRead(touchSensor6);
  int buttonState7 = CS.digitalRead(touchSensor7);
  int buttonState8 = CS.digitalRead(touchSensor8);
  int buttonState9 = CS.digitalRead(touchSensor9);
  int buttonState10 = CS.digitalRead(touchSensor10);
  
  int buttonState11 = CS.digitalRead(touchSensor11);   //11
  int buttonState12 = CS.digitalRead(touchSensor12);
  int buttonState13 = CS.digitalRead(touchSensor13);
  int buttonState14 = CS.digitalRead(touchSensor14);
  int buttonState15 = CS.digitalRead(touchSensor15);
  int buttonState16 = CS.digitalRead(touchSensor16);
  int buttonState17 = CS.digitalRead(touchSensor17);
  int buttonState18 = CS.digitalRead(touchSensor18);
  int buttonState19 = CS.digitalRead(touchSensor19);
  int buttonState20 = CS.digitalRead(touchSensor10);
  
  int buttonState21 = CS.digitalRead(touchSensor21);   //21
  int buttonState22 = CS.digitalRead(touchSensor22);
  int buttonState23 = CS.digitalRead(touchSensor23);
  int buttonState24 = CS.digitalRead(touchSensor24);
  int buttonState25 = CS.digitalRead(touchSensor25);
  int buttonState26 = CS.digitalRead(touchSensor26);
  int buttonState27 = CS.digitalRead(touchSensor27);
  int buttonState28 = CS.digitalRead(touchSensor28);
  int buttonState29 = CS.digitalRead(touchSensor29);
  int buttonState30 = CS.digitalRead(touchSensor30);
  
  int buttonState31 = CS.digitalRead(touchSensor31);   //31
  int buttonState32 = CS.digitalRead(touchSensor32);
  int buttonState33 = CS.digitalRead(touchSensor33);
  int buttonState34 = CS.digitalRead(touchSensor34);
  int buttonState35 = CS.digitalRead(touchSensor35);
  int buttonState36 = CS.digitalRead(touchSensor36);
  int buttonState37 = CS.digitalRead(touchSensor37);
  int buttonState38 = CS.digitalRead(touchSensor38);
  int buttonState39 = CS.digitalRead(touchSensor39);
  int buttonState40 = CS.digitalRead(touchSensor40);
  
  int buttonState41 = CS.digitalRead(touchSensor41);   //41
  int buttonState42 = CS.digitalRead(touchSensor42);
  int buttonState43 = CS.digitalRead(touchSensor43);
  int buttonState44 = CS.digitalRead(touchSensor44);
  int buttonState45 = CS.digitalRead(touchSensor45);
  int buttonState46 = CS.digitalRead(touchSensor46);
  int buttonState47 = CS.digitalRead(touchSensor47);
  int buttonState48 = CS.digitalRead(touchSensor48);
  int buttonState49 = CS.digitalRead(touchSensor49);
  int buttonState50 = CS.digitalRead(touchSensor50);
  
  int buttonState51 = CS.digitalRead(touchSensor51);   //51
  int buttonState52 = CS.digitalRead(touchSensor52);
  int buttonState53 = CS.digitalRead(touchSensor53);
  int buttonState54 = CS.digitalRead(touchSensor54);
  int buttonState55 = CS.digitalRead(touchSensor55);
  int buttonState56 = CS.digitalRead(touchSensor56);
  int buttonState57 = CS.digitalRead(touchSensor57);
  int buttonState58 = CS.digitalRead(touchSensor58);
  int buttonState59 = CS.digitalRead(touchSensor59);
  int buttonState60 = CS.digitalRead(touchSensor60);

  int buttonState61 = CS.digitalRead(touchSensor61);   //61
  int buttonState62 = CS.digitalRead(touchSensor62);
  int buttonState63 = CS.digitalRead(touchSensor63);
  int buttonState64 = CS.digitalRead(touchSensor64);
  int buttonState65 = CS.digitalRead(touchSensor65);
  int buttonState66 = CS.digitalRead(touchSensor66);
  int buttonState67 = CS.digitalRead(touchSensor67);
  int buttonState68 = CS.digitalRead(touchSensor68);
  int buttonState69 = CS.digitalRead(touchSensor69);
  int buttonState70 = CS.digitalRead(touchSensor70);
  
  int buttonState71 = CS.digitalRead(touchSensor71);   //71
  int buttonState72 = CS.digitalRead(touchSensor72);
  int buttonState73 = CS.digitalRead(touchSensor73);
  int buttonState74 = CS.digitalRead(touchSensor74);
  int buttonState75 = CS.digitalRead(touchSensor75);
  int buttonState76 = CS.digitalRead(touchSensor76);
  int buttonState77 = CS.digitalRead(touchSensor77);
  int buttonState78 = CS.digitalRead(touchSensor78);
  int buttonState79 = CS.digitalRead(touchSensor79);
  int buttonState80 = CS.digitalRead(touchSensor80);
  
  int buttonState81 = CS.digitalRead(touchSensor81);   //81
  int buttonState82 = CS.digitalRead(touchSensor82);
  int buttonState83 = CS.digitalRead(touchSensor83);
  int buttonState84 = CS.digitalRead(touchSensor84);
  int buttonState85 = CS.digitalRead(touchSensor85);
  int buttonState86 = CS.digitalRead(touchSensor86);
  int buttonState87 = CS.digitalRead(touchSensor87);
  int buttonState88 = CS.digitalRead(touchSensor88);
  int buttonState89 = CS.digitalRead(touchSensor89);
  int buttonState90 = CS.digitalRead(touchSensor90);
  
  int buttonState91 = CS.digitalRead(touchSensor91);   //91
  int buttonState92 = CS.digitalRead(touchSensor92);
  int buttonState93 = CS.digitalRead(touchSensor93);
  int buttonState94 = CS.digitalRead(touchSensor94);
  int buttonState95 = CS.digitalRead(touchSensor95);
  int buttonState96 = CS.digitalRead(touchSensor96);
  int buttonState97 = CS.digitalRead(touchSensor97);
  int buttonState98 = CS.digitalRead(touchSensor98);
  int buttonState99 = CS.digitalRead(touchSensor99);
  int buttonState100 = CS.digitalRead(touchSensor100);

... so kürzen, dass ich möglichst wenig Speicherplatz benötige?

Vielen Dank schon mal im Voraus leon2212

You can use an array to make it easier to read the code for humans.

But it will not take up less memory. No matter how you do it, the code needs a storage for each variable you want to use.

Google : (sorry !)

Sie können ein Array verwenden, um es einfacher, um den Code für den Menschen zu lesen.

Aber es wird nicht weniger Speicherplatz benutzen. Egal, wie Sie es tun, muss der Code einen Speicher für jede Variable die Sie verwenden möchten.

If CS.digitalRead() returns only 1 or 0, then each individual buttonState needs only one bit. You could store 8 buttonStates in a single byte, so 13 bytes total to store 100+ buttonStates.

You can also read all the sensors in a loop, like this:

for (int i=1; i<101; i++) {
   int k = CS.digitalRead(i);
   store_the_value(i,k);
   }

;

A packed struct will do what is asked for best:

struct buttonstates {
    int s1:1;
    int s2:1;
    int s3:1;
   int s4:1;
//...
    int s100:1;
};
//...
buttonStates.s4 = CS.digitalRead(touchSensor4);

But using arrays would allow factoring the code instead of having 100's of inline statements.

    int s1:1;
    int s2:1;
    int s3:1;
   int s4:1;

It hardly makes sense to use a two byte data type to hold one bit.

PaulS:     int s1:1;     int s2:1;     int s3:1;   int s4:1;

It hardly makes sense to use a two byte data type to hold one bit.

I'm just using the type that he used. I can't guess what he's doing with it. But it will be stored one bit per element. So the struct size will be just over 100/8 = 13 bytes.

A small problem with the struct approach, with the (signed) int type for a single bit, is that a “1” bit gets read out as -1, due to sign extension.

Besides, it is a pain to type all that! I would just index the bits in a char array directly.

jremington:
A small problem with the struct approach, with the (signed) int type for a single bit, is that a “1” bit gets read out as -1, due to sign extension.

That would be acceptable for use as a boolean value.

My BitBool library makes this an easy task: http://forum.arduino.cc/index.php?topic=128407.0

#include <BitBool.h>

BitBool<100> buttons;

//...


void loop(){

  for( char index = 0 ; index < buttons.BitCount ; index++ ){

    buttons[ index ] = CS.digitalRead( index + 1 );
  }
}

This is assuming that the buttons start at 1, not zero like an array. However I’d assume that CS.digitalRead would have its first pin indexed as zero.

Hab ich was verpasst oder warum antworten hier alle auf ne deutsche Frage in Englisch?

ElEspanol: Hab ich was verpasst oder warum antworten hier alle auf ne deutsche Frage in Englisch?

Es tut uns leid mein Deutsch ist Blumentopf!

ElEspanol: Hab ich was verpasst oder warum antworten hier alle auf ne deutsche Frage in Englisch?

Wer nachts um eins fragt, sollte dankbar sein, dass selbst im deutschen Forum bis morgens um vier schon Antworten da sind.

Frohe Ostern, übrigens.

Der BitBool - Ansatz sieht erstmal interessant aus, wenn es mehr als 32 sind. (Bis 32 geht auch das Arduino-Standard bitWrite(long data, byte bit, boolean value); und seine Verwandten).

Deine erste Frage hingegen ist durch korrektes Programmieren zu lösen:

const byte touchSensor1 = 1;

braucht in der Regel gar keinen Speicherplatz.

Wenn du in Schleifen arbeitest, ist es günstig, wenn du die Pin-Nummer leicht ausrechnen kannst, statt sie aus einer Tabelle im RAM abzulesen.