ciao
per una applicazione che sto sviluppando, mi sono imbattuto in uno strano comportamento di un sensore induttivo (cinese), quello che accade è che quando muovo un oggetto metallico davanti al sensore lentamente ho una doppia eccitazione della elettrovalvola collegata attraverso degli shift register, mentre se lo muovo più velocemente questo non accade, c'è una spiegazione?
Grazie
Stefano
//versione con utilizzo dell'interrupts versione 2
/*
versione del 24092014
stato: exe
*/
//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
byte dataPinOUT = 11; //Pin connected to DS of 74HC595 14
byte latchPinOUT = 8;//Pin connected to ST_CP of 74HC595 12
byte clockPinOUT = 12;//Pin connected to SH_CP of 74HC595 11
//pin per shiftin
byte dataPinIN = 7;//Pin connected to Q8 of 4021 3
byte latchPinIN = 6;//Pin connected to P/SC of 4021 9
byte clockPinIN = 5;//Pin connected to CLOCK of 4021 10
//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; //100111113
byte Var1,Var2;
int n, p;//contatori
int tempo0;
int ritardo[11] = {
0, 20, 30, 45, 60, 60, 45, 30, 20, 0, 150};
int statosensore1;
byte pin_out = 4; //pin da collegare al pin_in della scheda della pinza shiftout
void setup()
{
//set pins to output so you can control the shift registers
//shiftout define pin modes
pinMode(dataPinOUT, OUTPUT);
pinMode(latchPinOUT, OUTPUT);
pinMode(clockPinOUT, OUTPUT);
digitalWrite(latchPinOUT, LOW);
//shiftin define pin modes
pinMode(dataPinIN, INPUT);
pinMode(latchPinIN, OUTPUT);
pinMode(clockPinIN, OUTPUT);
pinMode(2, INPUT_PULLUP);
attachInterrupt(0, sensore1, FALLING); //sensore induttivo collegato al pin 2
pinMode(pin_out, OUTPUT);
digitalWrite(pin_out, HIGH);
statosensore1 = 0; //stato iniziale del sensore
Var1 = B00000000;
Var2 = B00000000;
//sezione per shiftout spegne tutti i led
//delay(20);
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(10);
Serial.begin(115200);
}
void loop()
{
//lettura dello stato dei pin in input
//shiftin
//Pulse the latch pin:
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
//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);
/*
Serial.print(switchVar1, BIN);
Serial.print('\t');
Serial.println(switchVar2, BIN);
*/
/* //sezione per shiftout spegne tutti i led
Var1 = B00000000;
Var2 = B00000000;
//delay(20);
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(10);
*/
//questa sezione è il cuore e scopo del programma
//lo shiftin ha memorizzato lo stato dei 10 sensori HIGH o LOW
//per la verifica della presenza o meno della pianta nel condotto
n = 0;
//p=0;
if(switchVar1 == B00000000 && switchVar2 == B00000000)//per caricare il distributore al primo giro
{
digitalWrite(pin_out, LOW);
delay(500);
digitalWrite(pin_out, HIGH);
}
//primo ciclo do per le prime 5 posizioni 1-5
do
{
if(statosensore1 == 1)
{
do
{
if (bitRead(switchVar1,n) == LOW) //verifica il valore del bit nella variabile 1
//LOW pianta presente HIGH pianta assente
{
delay(ritardo[n]); //ritardo tra una fila e l'altra
bitWrite(Var1, n, HIGH);//scrive nella varialbile per eccittare l'elettrovalvola
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 eccita l'elettrovalvola
digitalWrite(latchPinOUT, HIGH); //Pull latch HIGH to stop sending data
delay(ritardo[10]); //ritardo per lo scarico della pianta
bitWrite(Var1, n, LOW);//scrive nella varialbile per diseccittare l'elettrovalvola
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
statosensore1 = 0;
}
n++;
}
while(statosensore1 == 1 && n < 5);
}
}
while(n < 5);
//inizio secondo ciclo do 6-10
do
{
if (statosensore1 == 1)
{
do
{
if (bitRead(switchVar2, n-5) == LOW) //verifica il valore del bit nella variabile 1
{
delay(ritardo[n]); //ritardo tra una fila e l'altra
bitWrite(Var2,n-5,HIGH);//eccita l'elettrovalvola
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(ritardo[10]); //ritardo per lo scarico della pianta
bitWrite(Var2,n-5,LOW);//diseccita l'elettrovalvola
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
statosensore1 = 0;
// p++;
//Serial.println(n);
//Serial.println(ritardo+(9-n)*deltaR);
}
n++;
}
while(statosensore1 == 1 && n < 10);
//p=0;
} nc
}
while (n < 10);
}//fine loop
void sensore1() //pin 2 a cui attaccare il sensore induttivo
{
statosensore1 = 1;
}
//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;
}