where does these ones come from ?

hello guys,
i need please to know from where does these 1s come from in this shift in application?
here are the codes and i am not clicking anything and i see ones coming in the monitor check the attachment file please

//define where your pins are
int latchPin = 4;
int dataPin = 6;
int clockPin = 2;

int ledRed = 10;
int ledYellow = 9;
int ledGreen = 8;

int buttonState1 = 0;         // current state of the button
int lastButtonState1 = 0;

int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;

int buttonState3 = 0;         // current state of the button
int lastButtonState3 = 0;

//Define variables to hold the data 
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar1 = 0;  //01001000

//define an array that corresponds to values for each 
//of the shift register's pins
char note2sing[] = {
  'C', 'd', 'e', 'f', 'g', 'a', 'b', 'c'}; 


void setup() {
  //start serial
  Serial.begin(9600);

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);
  
  pinMode(ledRed, OUTPUT); 
  pinMode(ledYellow, OUTPUT); 
  pinMode(ledGreen, OUTPUT); 

}

void loop() {

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin,1);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(20);
  //set it to 0 to transmit data serially  
  digitalWrite(latchPin,0);

  //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);

  //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
  //reading  
  Serial.println(switchVar1, 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
    //register.
    if (switchVar1 & (1 << n) ){
      //print the value of the array location
      
      switch (switchVar1) {
      case B1000000:
        buttonState1 = HIGH;
        if(buttonState1 != lastButtonState1){
          digitalWrite(ledRed, HIGH);
          Serial.println(switchVar1);
        } 
        lastButtonState1 = buttonState1;
        break;
      case B0100000:
        buttonState2 = HIGH;
        if(buttonState2 != lastButtonState2){
          digitalWrite(ledYellow, HIGH);
        }
        lastButtonState2 = buttonState2;
        break;
      case B0010000:
        buttonState3 = HIGH;
        if(buttonState3 != lastButtonState3){
          digitalWrite(ledGreen, HIGH);
        }
        lastButtonState3 = buttonState3;
        break;
       default: 
        // if nothing else matches, do the default
        Serial.println("EMptyyyyyyyyyyyyyy"); 
        }
      }
    }

//This following is a away to examine the whole
//byte at once and create combinations
//of settings.

//By passing the switchVar1 variable to
//a "switch" statement and comparing it against
//a set nemerical value (written in binary)
//you can create special cases

  

//white space
Serial.println("-------------------");
//delay so all these print satements can keep up. 
delay(500);

}

//------------------------------------------------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);
    delayMicroseconds(0.2);
    temp = digitalRead(myDataPin);
    if (temp == HIGH) { // here i added == HIGH, i guess it should be right ?
      pinState = 1;
      //set the bit to 0 no matter what (this comment error is from the arduino.cc example on shift in)
      myDataIn = myDataIn | (1 << i);
    }
    else {
      //turn it off -- only necessary for debuging
     //print statement since myDataIn starts as 0
      pinState = 0;
    }

    digitalWrite(myClockPin, 1);

  }

  return myDataIn;
}

shift results monitor.jpg

Shift register MC14014B-D-23133.pdf (146 KB)

Are there any hardware connected to arduino and how are they wired? I surely can't see those through my crystal ball.

liudr:
Are there any hardware connected to arduino and how are they wired? I surely can’t see those through my crystal ball.

ok here is a picture for the circuit but please ignore any error in wiring the pushbuttons, because the real circuit is well connected by in fritzing i get confuse when wiring the push buttons because it has 4 pins but in my real circuit i use buttons that has 2 pins
by the way when i touch the resisters connected to the pins the value becomes 0 !!

i added the datasheet of my register

Shift register MC14014B-D-23133.pdf (146 KB)

firashelou:
here is a picture for the circuit but please ignore any error in wiring the pushbuttons

Floating pins could be a likey cause for problem. How is it you wired the buttons then?

That fritzing ! sketch is a real mess and reason for people to hate Fritzing !

Use different wire colors (it's easy to change these and to select different colors) for different functions.
For instance of red for power, black for GND, green for Arduino output, yellow for Arduino input there's enough colors to pick.
Think a bit longer when making such a sketch.
Draw straight lines.
If you have to cross lines, cross with a 90 degrees angle.

The way it looks now, is like an explosion just happened.
Like wires were randomly thrown from your breadboard, picked up where they landed and connected to the Arduino.

this is a picture on how it’s wired

pushbutton.jpg

MAS3:
That fritzing ! sketch is a real mess and reason for people to hate Fritzing !

Use different wire colors (it's easy to change these and to select different colors) for different functions.
For instance of red for power, black for GND, green for Arduino output, yellow for Arduino input there's enough colors to pick.
Think a bit longer when making such a sketch.
Draw straight lines.
If you have to cross lines, cross with a 90 degrees angle.

The way it looks now, is like an explosion just happened.
Like wires were randomly thrown from your breadboard, picked up where they landed and connected to the Arduino.

lol yes your right i never change color because i didn't know, but now you told me

so guys anything that can help please?
i am getting frustrated and i must do it :frowning: it's been 3 days trying to figure this out !!

[quote author=James C4S]
Floating pins could be a likey cause for problem.[/quote]

That should have helped.
De-float those pins.
Hint:
You de-floated the pins that do have a switch connected.

MAS3:

[quote author=James C4S]
Floating pins could be a likey cause for problem.

That should have helped.
De-float those pins.
Hint:
You de-floated the pins that do have a switch connected.

[/quote]

i did put a resistors 10K and i tryed to make a debounce in the codes but i donno if the debounce is right ?

If you wired as your fritzing diagram then the buttons are wired wrong. I bet you wired differently since you said yours have only two contacts. What I recommend is to remove 3 buttons and have 1 button. Take nice photos of your actual setup at several angles and make sure all wires are shown with where they start and end. It is your actual circuit that is wrong. You need to post it.

firashelou:
i did put a resistors 10K and i tryed to make a debounce in the codes but i donno if the debounce is right ?

I gave you a hint by telling the switches are indeed not floating.
So something else must be floating then, right ?

You did indeed create some debounce mechanism by keeping track of some last button states.
But do you know how much time has passed between 2 checks ?
I don't see some time related item in the check (the switch... case), but i might be missing something.

As you already have set up a serial connection, use it for debugging.
Have a peek to see how much time it takes to run your sketch just one single time (and be surprised).
See if this is enough time to do some debouncing in case you didn't hide a timer somewhere that i missed.

liudr:
If you wired as your fritzing diagram then the buttons are wired wrong. I bet you wired differently since you said yours have only two contacts. What I recommend is to remove 3 buttons and have 1 button. Take nice photos of your actual setup at several angles and make sure all wires are shown with where they start and end. It is your actual circuit that is wrong. You need to post it.

ok now what i did, after seeing all the comments about the floating and maybe breadboard problem or wiring, i thought about building and soldering the circuit for buttons and the shift register and here are the picture :
but the same problem is there and still getting these results on monitor and these ones !! the P7 is always 1 why ?! i connected it to ground but it make all the circuit go to switchVar1 = 0 !!

and these are the codes i am using :

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

//Define variables to hold the data 
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar1 = 72;  //01001000

void setup() {
  //start serial
  Serial.begin(9600);

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);

}

void loop() {

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin,1);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(20);
  //set it to 0 to transmit data serially  
  digitalWrite(latchPin,0);

  //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);

  //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
  //reading  
  Serial.println(switchVar1, BIN);

//white space
Serial.println("-------------------");
//delay so all these print satements can keep up. 
delay(500);

}

//------------------------------------------------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);
    delayMicroseconds(0.2);
    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(pinState);
    //Serial.print("     ");
    //Serial.println (dataIn, BIN);

    digitalWrite(myClockPin, 1);

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

results monitor.jpg

MAS3:

firashelou:
i did put a resistors 10K and i tryed to make a debounce in the codes but i donno if the debounce is right ?

I gave you a hint by telling the switches are indeed not floating.
So something else must be floating then, right ?

You did indeed create some debounce mechanism by keeping track of some last button states.
But do you know how much time has passed between 2 checks ?
I don't see some time related item in the check (the switch... case), but i might be missing something.

As you already have set up a serial connection, use it for debugging.
Have a peek to see how much time it takes to run your sketch just one single time (and be surprised).
See if this is enough time to do some debouncing in case you didn't hide a timer somewhere that i missed.

well i tried many many many places but i gave up =( and i need to do this !!

Can you confirm you have these wired (cannot tell in the picture)

int latchPin = 8;
int dataPin = 9;
int clockPin = 7;

Don't give up, it's not that hard.
You're just hitting the same wall over again, but are blaming an other wall.

The problem is not in the switches (as far as can be seen).
You are seeing ones at places where you do not expect them to be.
What part of your hardware is producing/reporting these ones ?
How does it decide to send a zero or a one ?
Are you sure you're setting it what it is reporting ?

About debouncing:
Do you know much time passes between setting lastbuttonState and buttonState ?
Need to know that to do a decent debounce.

About the rest of your sketch:
There's a bunch of comments in it, describing what it is supposed to do.
I'm not entirely sure that what is commented is actually what happens, because these comments are conflicting.
For instance it states that leading zeroes are dimissed.
But the sketch does check for these leading zeroes.

Then, are you going to make it impossible to press multiple switches in the same time ?
If not, maybe you should check for multiple pressed switches.

There must be a way to check one single bit.
If you figure out a way to isolate single bits, you can check just these bits.
You can make the switch... case fall through, doing multiple checks on a variable in one run.

LarryD:
Can you confirm you have these wired (cannot tell in the picture)

int latchPin = 8;
int dataPin = 9;
int clockPin = 7;

confirmed

MAS3:
Don't give up, it's not that hard.
You're just hitting the same wall over again, but are blaming an other wall.

The problem is not in the switches (as far as can be seen).
You are seeing ones at places where you do not expect them to be.
What part of your hardware is producing/reporting these ones ?
How does it decide to send a zero or a one ?
Are you sure you're setting it what it is reporting ?

About debouncing:
Do you know much time passes between setting lastbuttonState and buttonState ?
Need to know that to do a decent debounce.

About the rest of your sketch:
There's a bunch of comments in it, describing what it is supposed to do.
I'm not entirely sure that what is commented is actually what happens, because these comments are conflicting.
For instance it states that leading zeroes are dimissed.
But the sketch does check for these leading zeroes.

Then, are you going to make it impossible to press multiple switches in the same time ?
If not, maybe you should check for multiple pressed switches.

There must be a way to check one single bit.
If you figure out a way to isolate single bits, you can check just these bits.
You can make the switch... case fall through, doing multiple checks on a variable in one run.

actully yes the comments but it's not me that made it, simply from this website is the mistake !!
and about the multiple check i don't know how to do it in this case !! and the time of debouncing must be pretty fast so ?

delayMicroseconds(0.2);

Syntax
delayMicroseconds(us)
Parameters
us: the number of microseconds to pause (unsigned int)

LarryD:

delayMicroseconds(0.2);

Syntax
delayMicroseconds(us)
Parameters
us: the number of microseconds to pause (unsigned int)

these mistakes are from the codes on this site not mine, so what do i have to put instead (2) ?