ciao
sotto tutto lo sketch dove non riesco ad avere i pin 2, 3 interrupt alto, dove sbaglio?
molto è commentato per introdurre poco alla volta le funzionalità
grazie
//20210911
#include <Adafruit_MCP23X17.h>
Adafruit_MCP23X17 mcp;
byte EV[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; // MCP23XXX pin EV is attached to
//display
//#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
//byte numrig = 2;
//byte numcol = 16;
//byte n, i;
//LiquidCrystal_I2C lcd(0x27, numcol, numrig); // set the LCD address to 0x27 for a 16 chars and 2 line display
//button reset encoder
#define buttonPin 5 // the number of the pushbutton pin
byte buttonState = 0; // variable for reading the pushbutton status
//
////pin per shiftout
//const byte dataPinOUT = 11; //Pin connected to DS of 74HC595 14
//const byte latchPinOUT = 10;//Pin connected to ST_CP of 74HC595 12
//const byte clockPinOUT = 12;//Pin connected to SH_CP of 74HC595 11
//const byte OEPinOUT = 9;//Pin connected to OE of 74HC595 13
//
////pin per shiftin
//const byte dataPinIN = 7;//Pin connected to Q8 of 4021 3
//const byte latchPinIN = 6;//Pin connected to P/SC of 4021 9
//const 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, switchVar2;
//byte Var1, Var2;
//
//unsigned long tempo[2] = {0, 0}; //vettore memorizza l'istante di tempo in cui accade l'evento
//
//int n, j, k;//contatore
boolean flag;
#define encoder_PinA 2 //pin collegato al sensore pnp
#define encoder_PinB 3 //pin collegato al sensore pnp
volatile unsigned int encoder0Pos = 0; //volatile byte statosensore;
volatile unsigned int encoder0PosNew = 0; //volatile byte statosensore;
void setup()
{
// //display
// lcd.init(); // initialize the lcd
// lcd.backlight();
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
buttonState = 0; // variable for reading the pushbutton status
Serial.begin(9600);
//while (!Serial);
// Serial.println("MCP23xxx Blink Test!");
// // uncomment appropriate mcp.begin
// if (!mcp.begin_I2C())
// {
// //if (!mcp.begin_SPI(CS_PIN)) {
// Serial.println("Error.");
// while (1);
// }
// Serial.println("Looping...");
// configure pin for MCP23017 output
for (byte i = 0; 15; i++)
{
mcp.pinMode(EV[i], OUTPUT);
mcp.digitalWrite(EV[i], HIGH);
}
// //set pins to output so you can control the shift registers
// //shiftout define pin modes
// pinMode(dataPinOUT, OUTPUT);
// pinMode(latchPinOUT, OUTPUT);
// pinMode(clockPinOUT, OUTPUT);
// pinMode(OEPinOUT, OUTPUT);
// digitalWrite(OEPinOUT, LOW);
//sensore induttivo ruota
pinMode(encoder_PinA, INPUT_PULLUP);
digitalWrite(encoder_PinA, HIGH); //stato iniziale del sensore
attachInterrupt(0, sensor_A, FALLING); //sensore induttivo collegato al pin 2
pinMode(encoder_PinB, INPUT_PULLUP);
digitalWrite(encoder_PinB, HIGH); //stato iniziale del sensore
attachInterrupt(1, sensor_B, FALLING); //sensore induttivo collegato al pin 3
//
//
// j = 0;
// k = 0;
// n = 0;
// Var1 = B00000000;
// Var2 = B00000000;
// switchVar1 = B00000000;//6_10
// switchVar2 = B00000000;//1_5
//
// //sezione per shiftout spegne tutti i led
// digitalWrite(latchPinOUT, LOW); //Pull latch LOW to start sending data
// shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, B00000000); //Send the data byte 2
// shiftOut(dataPinOUT, clockPinOUT, MSBFIRST, B00000000); //Send the data byte 1
// digitalWrite(latchPinOUT, HIGH); //Pull latch HIGH to stop sending data
flag = 0;
delay(1000);
}
void loop()
{
//reset encoder
buttonState = digitalRead(buttonPin); //check if the pushbutton is pressed. If it is, the buttonState is HIGH:
// lcd.setCursor(0, 0);
// lcd.print(encoder0PosNew);
if (buttonState == HIGH)
{
encoder0PosNew = 0;
}
if (encoder0Pos != encoder0PosNew)
{
Serial.println (encoder0Pos); // use for debugging - remember to comment out
Serial.println ("DENTRO if"); // use for debugging - remember to comment out
}
encoder0PosNew = encoder0Pos;
delay(100);
switch (encoder0PosNew)
{
case 5:
while (flag = 1)
{
mcp.digitalWrite(EV[0], HIGH);
Serial.println (encoder0PosNew); // use for debugging - remember to comment out
Serial.println ("DENTRO switch"); // use for debugging - remember to comment out
flag = 0;
}
break;
}
}
//sezione lettura sensori induzione
void sensor_A()
{
// lcd.clear();
if (digitalRead(encoder_PinA) == HIGH)// look for a low-to-high on channel A
{
if (digitalRead(encoder_PinB) == LOW)// check channel B to see which way encoder is turning
{
encoder0Pos = encoder0Pos + 1; // CW
Serial.println("x1");
}
else
{
encoder0Pos = encoder0Pos - 1; // CCW
Serial.println("x2");
}
}
// must be a high-to-low edge on channel A
else
{
if (digitalRead(encoder_PinB) == HIGH) // check channel B to see which way encoder is turning
{
encoder0Pos = encoder0Pos + 1; // CW
Serial.println("x3");
}
else
{
encoder0Pos = encoder0Pos - 1; // CCW
Serial.println("x4");
}
}
//Serial.println (encoder0Pos); // use for debugging - remember to comment out
}
void sensor_B()
{
// lcd.clear();
if (digitalRead(encoder_PinB) == HIGH) // look for a low-to-high on channel B
{
if (digitalRead(encoder_PinA) == HIGH) // check channel A to see which way encoder is turning
{
encoder0Pos = encoder0Pos + 1; // CW
Serial.println("x5");
}
else
{
encoder0Pos = encoder0Pos - 1; // CCW
Serial.println("x6");
}
}
// must be a high-to-low edge on channel A
else
{
if (digitalRead(encoder_PinA) == LOW)// check channel B to see which way encoder is turning
{
encoder0Pos = encoder0Pos + 1; // CW
Serial.println("x7");
}
else
{
encoder0Pos = encoder0Pos - 1; // CCW
Serial.println("x8");
}
}
//Serial.println (encoder0Pos); // use for debugging - remember to comment out
}