ciao
per leggere lo stato LOW di 6 sensori voglio utilizzare shiftin, sto provando con un solo sensore ma non riesco a rilevare lo stato LOW, la durata LOW è forse troppo bassa?
int latchPinIN = 8;//Pin connected to P/SC of 4021
int dataPinIN = 9;//Pin connected to Q8 of 4021
int clockPinIN = 7;//Pin connected to CLOCK of 4021
//Define variables to hold the data
//for shift register.
//starting with a non-zero numbers can help troubleshoot
byte switchVar1 = 78; //01001110
int i; //contatore
void setup()
{
//shiftin define pin modes
pinMode(latchPinIN, OUTPUT);
pinMode(clockPinIN, OUTPUT);
pinMode(dataPinIN, INPUT);
unsigned long time;
Serial.begin(9600);
}
void loop()
{
digitalWrite(latchPinIN,1); //set it to 1 to collect parallel data, wait
delayMicroseconds(50);
digitalWrite(latchPinIN,0); //set it to 0 to transmit data serially
switchVar1 = shiftIn(dataPinIN, clockPinIN);
Serial.println(switchVar1,BIN);
}
//------------------------------------------------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(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;
}
con questo output
...
11111111
11111111
11111111
...
per come ho fatto i collegamenti mi aspetto questo
...
11011111
...
al verificarsi del segnale LOW allegato
grazie
stefano
20120205-0001.pdf (197 KB)