I have a frequency coming through which I am countint by attaching an interrupt, delaying 1000, then detaching. This was working very reliably.
The frequency is from a hall effect sensor on a fan that was creating noise, so I pass it through an opto isolator to keep the two circuits seperate. On the destination side, the Input Pullup pin is pulled to ground when the transistor is activated. I have measured the frequency between the pin and ground and see its being pulled at the correct frequency down to 0.5v then up to 5.5v. After making these changes to put in the opto (and moving everything to an enclosure etc), it is not working and returns counts that are almost randlm. But, I can measure the signal the arduino sees and it should be perfect.
What should I look at? Things broke after moving to a new case, but after putting in the opto I monitor the signal and it should be super clear. Why is the interrupt not tripping likewise when it once
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int lstart=1;// 25; //Where to start drawing updated RPM
int rstart=65;//95;
int yRPM=33;//18; //row where RPM info is
String lowPN="9HV08"; //14,900
int lowmin=13900;
int lowmax=15300;
String PN;
String highPN="9HVA08"; //16,100
int highmin= 15700;
int highmax= 17100;
bool lpass=true;
bool rpass=true;
bool failure=false;
int min;
int max;
int lsensorpin = 2;
int rsensorpin= 3;
int rgled=4;
int rrled=5;
int gbut=7;
int rbut=6;
int lgled=8;
int lrled=9;
int lrelaypin= 10;
int rrelaypin=11;
int flipperpin=15; //A1
bool redexit=false;
int x=0;
bool flipper=false;
int timestamp;
unsigned long L=0;
unsigned long R=0;
int lRPM=0;
int rRPM=0;
void setup() {
pinMode(lsensorpin, INPUT_PULLUP); //2
pinMode(rsensorpin, INPUT_PULLUP); //3
pinMode(rgled, OUTPUT); //4
pinMode(rrled, OUTPUT); //5
pinMode(gbut, INPUT_PULLUP); //6
pinMode(rbut, INPUT_PULLUP); //7
pinMode(lgled, OUTPUT); //8
pinMode(lrled, OUTPUT); //9
pinMode(lrelaypin, OUTPUT); //10
pinMode(rrelaypin, OUTPUT); //11
pinMode(flipperpin, INPUT_PULLUP); //15
//SDA18
//SCL19
// IDK WHy but screen needs this
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
//Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display(); //Show Adafruit logo 2 seconds
delay(500); // Pause for 2 seconds
display.clearDisplay();
//setupscreen(1); will be done under setflipper
setflipper(); //UNDOOOOOOOOOOOOOOO
fansoff();
lightdemo();
Serial.begin(115200);
//Serial.println("FInish Setup");
}
//void loop(){};
void loop() {
if (flipper!=digitalRead(flipperpin)) setflipper();
if ((failure==true) && (digitalRead(rbut)==false)){ //There was a failure and pressing red button to reset. If not pressing red it will go on to checking GO
setlights(0,0,0,0);
failure=false;
//SOMETHING RESETTING SCREEN BACK TO DISPLAYING MIN MAX
}
if ((failure==false) && (digitalRead(gbut)==false)){ //No previous failure and Green is go
setupscreen(1);
setlights(0,0,0,0);
L=0;
R=0;
initiateandmonitor();
//Serial.print("Got out of initiate with redexit=");
//Serial.println(redexit);
if (redexit==false){ //Spun up without red button
/*Serial.println("8 seconds later with fans");
timestamp=millis()+2000;
Serial.println("timestamp");
while (timestamp>millis()){
Serial.print("Pin:");
Serial.println(digitalRead(lsensorpin));
}
*/
//measure Left
attachl();
delay(1000);
detachl();
//measure right
attachr();
delay(1000);
detachr();
//shut down and measure/displauy
fansoff();
Serial.print("L:");
Serial.print(L);
Serial.print("R:");
Serial.print(R);
lRPM=L*30;
rRPM=R*30;
Serial.print("lRPM:");
Serial.print(lRPM);
Serial.print("rRPM:");
Serial.print(rRPM);
lpass= (lRPM<max && lRPM>min);
rpass= (rRPM<max && rRPM>min);
if (lpass==true && rpass==true){
failure=false;
setlights(1,0,1,0);
}else if (lpass==true && rpass==false){
failure=true;
setlights(1,0,0,1);
}else if (lpass==false && rpass==true){
failure=true;
setlights(0,1,1,0);
}else{
failure=true;
setlights(0,1,0,1);
}
setupscreen(2);
}
}
}
void setflipper(){
if (digitalRead(flipperpin)==true) { //flipper is down/off
flipper=true;
PN=lowPN;
min=lowmin;
max=lowmax;
}else{
flipper=false;
PN=highPN;
min=highmin;
max=highmax;
}
setupscreen(1);
}
void tickL() {
L=L+1;
}
void tickR() {
R=R+1;
}
void fansoff() {
digitalWrite(lrelaypin, true);
digitalWrite(rrelaypin, true);
}
void setlights(bool lg, bool lr, bool rg, bool rr) {
digitalWrite(lgled, lg);
digitalWrite(lrled, lr);
digitalWrite(rgled, rg);
digitalWrite(rrled, rr);
}
void lightdemo() {
setlights(1,0,1,0);
delay(1000);
setlights(0,1,0,1);
delay(1000);
setlights(0,0,0,0);
}
void fanson() {
digitalWrite(lrelaypin, false);
digitalWrite(rrelaypin, false);
}
void attachl(){
attachInterrupt(digitalPinToInterrupt(lsensorpin), tickL, RISING);
//attachInterrupt(digitalPinToInterrupt(rsensorpin), tickR, RISING);
}
void detachl(){
detachInterrupt(digitalPinToInterrupt(lsensorpin));
//detachInterrupt(digitalPinToInterrupt(rsensorpin));
}
void attachr(){
//attachInterrupt(digitalPinToInterrupt(lsensorpin), tickL, RISING);
attachInterrupt(digitalPinToInterrupt(rsensorpin), tickR, FALLING);
}
void detachr(){
//detachInterrupt(digitalPinToInterrupt(lsensorpin));
detachInterrupt(digitalPinToInterrupt(rsensorpin));
}
void initiateandmonitor(){
fanson();
delay(500);
redexit=false;
for(x = 0; x<160;x++){
if(digitalRead(rbut)==false){
redexit=true;
fansoff();
Serial.println("Breaking the fans");
setlights(0,1,0,1);
setupscreen(3);
break;
}else{
//timestamp=millis()+2000;
//Serial.println("timestamp");
//while (timestamp>millis()){
// Serial.print("Pin:");
// Serial.println(digitalRead(lsensorpin));
//}
delay(50);
if (x%20==0) setupscreen(3);
}
}
//if(x>159) Serial.println("Spun up with no red button");
}
//SCREENSTUFFFFFFF
void setupscreen(int y) { //y=1 for showing PN being tested. y=2 to display results
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
if (y==1){
//Lines
display.drawLine(0, 15, display.width()-1, 15, SSD1306_WHITE);
display.drawLine(0, 16, display.width()-1, 16, SSD1306_WHITE);
display.drawLine(62, 15, 62, 55, SSD1306_WHITE);
//Header
display.setCursor(0, 0);
display.print("PN:");
display.setCursor(50, 0);
display.print(PN);
//RPM HEaders
display.setTextSize(1);
display.setCursor(0, 18);
display.print("MIN:");
display.setCursor(70, 18);
display.print("MAX:");
//Print Min Max
display.setCursor(lstart, yRPM);
display.print(min);
display.setCursor(rstart, yRPM);
display.print(max);
}else if (y==2){
//Lines
display.drawLine(0, 15, display.width()-1, 15, SSD1306_WHITE);
display.drawLine(0, 16, display.width()-1, 16, SSD1306_WHITE);
display.drawLine(62, 15, 62, 55, SSD1306_WHITE);
//Header
display.setCursor(0, 0);
display.print("Left");
display.setCursor(68, 0);
display.print("Right");
//RPM HEaders
display.setTextSize(1);
display.setCursor(0, 18);
display.print("RPM:");
display.setCursor(70, 18);
display.print("RPM:");
//PN at bottom when printing RPM
display.setCursor(0,55);
display.print(String(PN));
display.setCursor(65,48);
display.print(min);
display.setCursor(65,55);
display.print(max);
Serial.println("Part is " + PN);
//Print RPM's
display.setTextSize(2);
display.setCursor(lstart, yRPM);
display.print(lRPM);
display.setCursor(rstart, yRPM);
display.print(rRPM);
}else if (y==3){ //fans spinning
if (redexit==true){
display.setTextSize(3);
display.setCursor(5,24);
display.print("STOP");
}else{
//Lines
display.drawLine(0, 15, display.width()-1, 15, SSD1306_WHITE);
display.drawLine(0, 16, display.width()-1, 16, SSD1306_WHITE);
//Header
display.setCursor(0, 0);
display.print("SPIN UP");
//Countdown
display.setTextSize(3);
display.setCursor(50,20);
display.print(8-(x/20));
}
}
display.display();
}