Pages: [1]   Go Down
Author Topic: read button states to Processing (shift registers)  (Read 2212 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I've looked through all sorts of tutorials and examples and tried loads of different snippets of code. Seems that I've ran into a wall and the resulting code is a complete mess.

Could somebody help me out a bit. I'm just starting with this physical programming so I'm not very experienced yet..

What i've got up and running so far: arduino is connected to three GD4021B 8bit parallel>serial registers(same chip as in the shiftIn tutorial); each of those to 8 buttons. I've also connected a few LEDs to the remaining digital pins just to test if i'm reading the buttons right.

What I would really like to do is:

*read all 24 button states from the shift registers (HIGH/LOW)
*send the data to Processing
*create variable for each button
PS. should be capable to expand it to 12chips with 96 buttons, so that I don't hit a buffer limit or something..

Here is my  Arduino code:
(haven't found anything useful from the Processing side)

//define where your pins are
int latchPin = 9;
int dataPin = 10;
int clockPin = 8;

//Define variables to hold the data
//for each shift register.
//starting with non-zero numbers can help
byte switchVar1 = 72;  //01001000
byte switchVar2 = 159; //10011111
byte switchVar3 = 159; //10011111

//define an array that has a place for the values of
//pins 1-7 (not 0) of the second shift register's
//pins. Not 0 because that will be used as a flag value
byte settingVal[] = {
  0, 0, 0, 0, 0, 0, 0};  

byte bit7 = 7;
byte bit6 = 6;
byte bit5 = 5;
byte bit4 = 4;
byte bit3 = 3;
byte bit2 = 2;
byte bit1 = 1;
byte bit0 = 0;

boolean zero, zero2, zero3;
boolean one, one2, one3;
boolean two, two2, two3;
boolean three, three2, three3;
boolean four, four2, four3;
boolean five, five2, five3;
boolean six, six2, six3;
boolean seven, seven2, seven3;

//a flag varible used to track whether the program
//is in a setting update mode or not
byte settingSwitch = 0;  

void setup() {
  //start serial

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, INPUT);
  //define led pins
  pinMode(2, OUTPUT); //blueLED1
  pinMode(3, OUTPUT); //blueLED2
  pinMode(4, OUTPUT); //whiteLED1
  pinMode(5, OUTPUT); //whiteLED2
  pinMode(6, OUTPUT); //redLED1
  pinMode(7, OUTPUT); //redLED2


void loop() {

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  //set it to 1 to collect parallel data, wait
  //set it to 0 to transmit data serially  

  //while the shift register is in serial mode
  //collect each shift register into a byte
  //the register attached to the chip comes in first
  switchVar1 = shiftIn(dataPin, clockPin);
  switchVar2 = shiftIn(dataPin, clockPin);
  switchVar3 = shiftIn(dataPin, clockPin);

  //Print out the results.
  //leading 0's at the top of the byte
  //(7, 6, 5, etc) will be dropped before
  //the first pin that has a high input

  Serial.println(switchVar1, BIN);
  Serial.println(switchVar2, BIN);
  Serial.println(switchVar3, BIN);

  //This for-loop steps through the byte
  //bit by bit which holds the shift register data
  //and if it was high (1) then it prints
  //the corresponding location in the array
  for (int n=0; n<=7; n++)
    //so, when n is 3, it compares the bits
    //in switchVar1 and the binary number 00001000
    //which will only return true if there is a
    //1 in that bit (ie that pin) from the shift
    if (switchVar1 & (1 << n) ){


    zero = getBit(switchVar1, bit0);

    if (zero) {
      digitalWrite(2, HIGH);   // blue LED 1
      Serial.println("led1.1HIGH");    // button from chip1, pin1 HIGH
    else {
      digitalWrite(2, LOW);
      Serial.println("led1.1LOW");    // button from chip1, pin1 LOW
    zero2 = getBit(switchVar2, bit0);

    if (zero2) {
      digitalWrite(4, HIGH);   // white LED 1
      Serial.println("led2.1HIGH");    // button from chip2, pin1 HIGH
    else {
      digitalWrite(4, LOW);
      Serial.println("led2.1LOW");  //chip2,pin2,0(low)

    break;  // break so it doesn't announce the buttons' states several times during a loop


  //white space
  //delay so all these print satements can keep up.


//------------------------------------------------end main loop

////// ----------------------------------------shiftIn function
///// just needs the location of the data pin and the clock pin
///// it returns a byte with each bit in the byte corresponding
///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0
byte shiftIn(int myDataPin, int myClockPin) {
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
  //we will be holding the clock pin high 8 times (0,..,7) at the
  //end of each time through the for loop

  //at the begining of each loop when we set the clock low, it will
  //be doing the necessary low to high drop to cause the shift
  //register's DataPin to change state based on the value
  //of the next bit in its serial information flow.
  //The register transmits the information about the pins from pin 7 to pin 0
  //so that is why our function counts down
  for (i=7; i>=0; i--)
    digitalWrite(myClockPin, 0);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      //set the bit to 0 no matter what
      myDataIn = myDataIn | (1 << i);
    else {
      //turn it off -- only necessary for debuging
      //print statement since myDataIn starts as 0
      pinState = 0;

    //Debuging print statements
    //Serial.print("     ");
    //Serial.println (dataIn, BIN);

    digitalWrite(myClockPin, 1);

  //debuging print statements whitespace
  //Serial.println(myDataIn, BIN);
  return myDataIn;

////// ----------------------------------------getBit
boolean getBit(byte myVarIn, byte whatBit) {
  boolean bitState;
  bitState = myVarIn & (1 << whatBit);
  return bitState;

« Last Edit: January 19, 2011, 04:44:56 am by equuu » Logged

Seattle, WA USA
Online Online
Brattain Member
Karma: 631
Posts: 50163
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I love useful code.
   if (switchVar1 & (1 << n) ){

This isn't.

 switchVar1 = shiftIn(dataPin, clockPin);
  switchVar2 = shiftIn(dataPin, clockPin);
  switchVar3 = shiftIn(dataPin, clockPin);
Arrays would be useful.

What I would really like to do is:

*read all 24 button states from the shift registers (HIGH/LOW)
Looks to me like you are doing that.

*send the data to Processing
Go ahead. What's stopping you?

 Serial.println(switchVar1, BIN);
  Serial.println(switchVar2, BIN);
  Serial.println(switchVar3, BIN);
Oh, you're already doing that.

*create variable for each button
In Processing? Go ahead. What's stopping you?

(haven't found anything useful from the Processing side)
There are plenty of examples of reading serial data in Processing. The question is what you want to do with that data.

Pages: [1]   Go Up
Jump to: