shift out non funziona

ciao a tutti..sto sperimentando l'uso dello shifout e mi ritrovo che con il codice seguente tutto funziona

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
const int pulsante=10;
int pulsantecont=0;
int memoria=1;
int buttonstate=0;
int lastButtonState = LOW;

//holders for infromation you're going to pass to shifting function
byte dataRED;

byte dataArrayRED[10];


void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode (pulsante,INPUT);
  Serial.begin(9600);

  //Arduino doesn't seem to have a way to write binary straight into the code 
  //so these values are in HEX.  Decimal would have been fine, too. 
  dataArrayRED[0] = 0xFE; //11111111
  dataArrayRED[1] = 0xFC; //11111110
  dataArrayRED[2] = 0xF8; //11111100
  dataArrayRED[3] = 0xF0; //11111000
  dataArrayRED[4] = 0xE0; //11110000
  dataArrayRED[5] = 0xC0; //11100000
  dataArrayRED[6] = 0x80; //11000000
  dataArrayRED[7] = 0xFF; //10000000
  dataArrayRED[8] = 0x00; //00000000
  dataArrayRED[9] = 0x40; //11100000

  


}

void loop() {
 buttonstate = digitalRead(pulsante);
 
  delay(20); // debounce 

  if (buttonstate != lastButtonState) {
    if (buttonstate == HIGH && memoria==1) {
      pulsantecont++ ;

      if (pulsantecont >9) pulsantecont =0 ;


 
    dataRED = dataArrayRED[pulsantecont];
    Serial.println(dataRED,DEC);
 
    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, 0);
    //move 'em out
   
    shiftOut(dataPin, clockPin, dataRED);
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
    delay(300);


  }}}

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut?
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {	
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}

ma se nel codice aggiungo un altro pulsante con il seguente codice non succede + nulla

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 6;
////Pin connected to DS of 74HC595
int dataPin = 7;
const int pulsante=10;
const int pulsante1=9;
int pulsantecont=0;
int memoria=1;
int buttonstate=0;
int buttonstate1=0;
int lastButtonState = LOW;
int lastButtonState1 = LOW;
//holders for infromation you're going to pass to shifting function
byte dataRED;

byte dataArrayRED[10];


void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode (pulsante,INPUT);
  Serial.begin(9600);

  //Arduino doesn't seem to have a way to write binary straight into the code 
  //so these values are in HEX.  Decimal would have been fine, too. 
  dataArrayRED[0] = 0xFE; //11111111
  dataArrayRED[1] = 0xFC; //11111110
  dataArrayRED[2] = 0xF8; //11111100
  dataArrayRED[3] = 0xF0; //11111000
  dataArrayRED[4] = 0xE0; //11110000
  dataArrayRED[5] = 0xC0; //11100000
  dataArrayRED[6] = 0x80; //11000000
  dataArrayRED[7] = 0xFF; //10000000
  dataArrayRED[8] = 0x00; //00000000
  dataArrayRED[9] = 0x40; //11100000

  


}

void loop() {
 buttonstate = digitalRead(pulsante);
 
  delay(20); // debounce 

  if (buttonstate != lastButtonState) {
    if (buttonstate == HIGH && memoria==1) {
      pulsantecont==2 ;

    }
      lastButtonState = buttonstate;
      
      buttonstate1 = digitalRead(pulsante1);
 
  delay(20); // debounce 

  if (buttonstate1 != lastButtonState1) {
    if (buttonstate1 == HIGH && memoria==1) {
      pulsantecont==8 ;
    }
lastButtonState1 = buttonstate1;
 
    dataRED = dataArrayRED[pulsantecont];
    Serial.println(pulsantecont);
 
    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, 0);
    //move 'em out
   
    shiftOut(dataPin, clockPin, dataRED);
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
    delay(300);


  }}}

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {	
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}

La variabile memoria al momento è superflua ma mi servirà successivamente..qualcuno ha idea di come posso risolvere?alla fine di tutto devo avere 10 pulsanti che richiamino il rispettivo numero nel dataarrayred...

Di primo acchito, vedo che non hai dichiarato pulsante1 come input.
Poi, che significa che non funziona più nulla?

leo72:
Di primo acchito, vedo che non hai dichiarato pulsante1 come input.
Poi, che significa che non funziona più nulla?

significa che non succede proprio più nulla...teoricamente anche se non ho dichiarato pulsante1 come input dovrebbe rilevarmi la pressione dell'altro pulsante invece..buio totale dei led

Hai indendato male il codice, se lo sistemi (dall'IDE, scegli Strumenti/Formattazione automatica) vedi che il secondo pulsante viene letto all'interno dell'if che controlla la pressione del primo e che la spedizione dei dati tramite shiftout è finita nel blocco di controllo di pulsante1. Ma siccome questo non viene letto, la shiftout non viene eseguita.

perfetto funziona..grazie :wink:

mattwolf:
perfetto funziona..grazie :wink:

Questo ti faccia capire che quando ci incaponiamo nel suggerire a gran voce di scrivere del codice indentato bene lo facciamo anche perché in questo modo si possono vedere meglio i vari blocchi logici e capire se qualcosa non è al posto giusto :wink: