ciao
riprendo questo post per avere una spiegazione di questo comportamento:
//codice sviluppato partendo dal lavoro dei sottoelencati autori
// Author : Carlyn Maw, Tom Igoe
// Notes : Code for using a CD4021B Shift Register
// Notes : Code for using a 74hc595 Shift Register
/*il seguente codice utilizza le funzioni shifout
e le funzioni shiftin, viene eseguito il controllo dello stato
dei contatti su shiftin e la memorizzazione
nei registri di shiftin e riprodotti sui led dalla funzione shiftout
il led sarà in parallelo alla elettrovalvola
*/
//pin per shiftout
//Pin connected to ST_CP of 74HC595
int latchPinOUT = 11;
//Pin connected to SH_CP of 74HC595
int clockPinOUT = 12;
////Pin connected to DS of 74HC595
int dataPinOUT = 10;
//pin per shiftin
//Pin connected to P/SC of 4021
int latchPinIN = 8;
//Pin connected to Q8 of 4021
int dataPinIN = 9;
//Pin connected to CLOCK of 4021
int clockPinIN = 7;
//Define variables to hold the data
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar1 = 78; //01001110
byte switchVar2 = 159; //10011111
byte Var1 = 128; //01001110
byte Var2 = 159; //10011111
int n=0; //contatore
int pinsensore=2; //pin a cui collegarare il sensore induttivo
int statosensore=LOW; //stato iniziale del sensore
void setup()
{
//set pins to output so you can control the shift registers
//shiftout define pin modes
pinMode(latchPinOUT, OUTPUT);
pinMode(clockPinOUT, OUTPUT);
pinMode(dataPinOUT, OUTPUT);
//shiftin define pin modes
pinMode(latchPinIN, OUTPUT);
pinMode(clockPinIN, OUTPUT);
pinMode(dataPinIN, INPUT);
pinMode(pinsensore,INPUT);
Serial.begin(9600);
}
void loop()
{
//lettura dello stato dei pin in input
//shiftin
//Pulse the latch pin:
//set it to 1 to collect parallel data
digitalWrite(latchPinIN,1);
//set it to 1 to collect parallel data, wait
delayMicroseconds(50);
//set it to 0 to transmit data serially
digitalWrite(latchPinIN,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(dataPinIN, clockPinIN);
switchVar2 = shiftIn(dataPinIN, clockPinIN);
//white space
// Serial.println("-------------------");
//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_IN");
// Serial.println(switchVar1, BIN);
// Serial.println(int(switchVar1));
// Serial.println("switchVar2_IN");
// Serial.println(switchVar2, BIN);
// Serial.println(int(switchVar2));
/*
//sezione per shiftout
//visualizza lo stato complessivo degli switch accendendo i led crrispondenti
digitalWrite(latchPinOUT, LOW); //Pull latch LOW to start sending data
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, switchVar2); //Send the data byte 2
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, switchVar1); //Send the data byte 1
digitalWrite(latchPinOUT, HIGH); //Pull latch HIGH to stop sending data
//delay so all these print satements can keep up.
delay(500);
//sezione per shiftout spegne tutti i led
Var1=0;
Var2=0;
digitalWrite(latchPinOUT, LOW); //Pull latch LOW to start sending data
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, Var2); //Send the data byte 2
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, Var1); //Send the data byte 1
digitalWrite(latchPinOUT, HIGH); //Pull latch HIGH to stop sending data
delay(500);
*/
//questa sezione è il cuore e scopo del programma
//lo shiftin memorizza lo stato dei 10 sensori HIGH o LOW
//per la verifica della presenza o meno della pianta nel condotto
//in questo caso simulato da 10 switch
n=0;
do
{
statosensore=digitalRead(2); //per verificare la posizione del bicchiere
// statosensoreold=digitalRead(2); //per verificare la posizione del bicchiere
// for (n=0; n<8; n++)
if (statosensore==HIGH)
{
if (bitRead(switchVar1,n)==HIGH) //verifica il valore del bit nella variabile 1
{
delay(10); //ritardo variabile a secondo della velocità della macchina
//eccita l'elettrovalvola
bitWrite(Var1,n,HIGH);
digitalWrite(latchPinOUT, LOW); //Pull latch LOW to start sending data
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, Var2); //Send the data byte 2
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, Var1); //Send the data byte 1
digitalWrite(latchPinOUT, HIGH); //Pull latch HIGH to stop sending data
// Serial.println("Var1_OUT");
// Serial.println(Var1, BIN);
// Serial.println(int(Var1));
delay(300); //ritardo per la completa eccitazione della bobina dell'elettrovalvola
//diseccita l'elettrovalvola
bitWrite(Var1,n,LOW);
digitalWrite(latchPinOUT, LOW); //Pull latch LOW to start sending data
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, Var2); //Send the data byte 2
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, Var1); //Send the data byte 1
digitalWrite(latchPinOUT, HIGH); //Pull latch HIGH to stop sending data
// statosensorenew=LOW;
delay(150);
}
n=n+1;
// Serial.println("n");
// Serial.println(n);
}
} while (n<8);
n=0;
do
{
statosensore=digitalRead(2);
// for (n=0; n<8; n++)
if (statosensore==HIGH)
{
if (bitRead(switchVar2,n)==HIGH) //verifica il valore del bit nella variabile 1
{
delay(10); //ritardo variabile a secondo della velocità della macchina
bitWrite(Var2,n,HIGH);
digitalWrite(latchPinOUT, LOW); //Pull latch LOW to start sending data
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, Var2); //Send the data byte 2
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, Var1); //Send the data byte 1
digitalWrite(latchPinOUT, HIGH); //Pull latch HIGH to stop sending data
// Serial.println("Var2_OUT");
// Serial.println(Var2, BIN);
// Serial.println(int(Var2));
delay(300);
bitWrite(Var2,n,LOW);
digitalWrite(latchPinOUT, LOW); //Pull latch LOW to start sending data
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, Var2); //Send the data byte 2
shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, Var1); //Send the data byte 1
digitalWrite(latchPinOUT, HIGH); //Pull latch HIGH to stop sending data
delay(150);
}
n=n+1;
// Serial.println("n");
// Serial.println(n);
}
} while (n<8);
} //parentesi di fine loop
//------------------------------------------------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;
}
il programma funziona bene, ad ogni evento (HIGH) sul pin 2 ho la accensione e spegnimento del led corrispondente allo stato 1 del bit, ora il problema è che dalla fine del loop al suo inizio perdo un evento, qualcuno mi spiega come mai e soprattutto un consiglio per risolvere
grazie
ciao