Go Down

Topic: play with buttons through mcp23s17 (Read 912 times) previous topic - next topic

Bamsen

need help of you clever people  :)

I'm trying to make an easy processing of many buttons that will eventually get functions. but my code gets either no reaksion or a lot of random letters.

think it has something to do with my array

Code: [Select]

#include <SPI.h>              // We use this library, so it must be called here.
#include <MCP23S17.h>         // Here is the new class to make using the MCP23S17 easy.

MCP inputchip(0);             // Instantiate an object called "inputchip" on an MCP23S17 device at address 1
MCP outputchip(1);            // Instantiate an object called "outputchip" on an MCP23S17 device at address 2

int Kp[80];
int KpG[80];

void setup() {
  Serial.begin(9600);            // Use the serial port
  inputchip.pinMode(0xFFFF);     // Use word-write mode to set all of the pins on inputchip to be inputs
  inputchip.pullupMode(0xFFFF);  // Use word-write mode to Turn on the internal pull-up resistors.
  inputchip.inputInvert(0xFFFF); // Use word-write mode to invert the inputs so that logic 0 is read as HIGH
  outputchip.pinMode(0x0000);    // Use word-write mode to Set all of the pins on outputchip to be outputs
}

void loop() {
          Serial.println("loop");
  int value;                        // declare an integer to hold the value temporarily.
 
  value = inputchip.digitalRead();  // reads the first input chip in word mode
   if (value > 0){
   KeyToInt(value, 1);               // convert readings and store in array
   }
      Serial.println(value);
     
// treats array
  for (int i = 0; i < sizeof(Kp); i++){
   if (Kp[i] > 0){
     // Press started or continue
     KpG[i] = KpG[i]++;
   }
   else if (Kp[i] == 0 && KpG[i] > 0){
     // Press finished
     Serial.println("Press finished nr.");
     Serial.println(i);
     Serial.println("number of loop");
     Serial.println(KpG[i]);
     
     KpG[i] = 0;
   }
  } 
// reset kp[] to next loop
char Kp[96];
}

/*
convert readings and store in array
*/
void KeyToInt(int y, int z){
    long x=32768;                            // largest word number 16 pin
  for (int count=16; count > 0; count--){    // loop through 16 pin
    if ((y - x) >= 0){
      y = y - x;
      // insert 1 in the array by key
      int cpin = (16 * z) + count;           // chip number * 16 buttons per chip + button number
      Kp[cpin] = 1;
    }
  x = x / 2;
  }
}


hope you can help me

Bamsen


Go Up