// this ver updated to work on GEN2 Meter U1 chip
// removed temperature and humidity code
// addding CLOCK time code



/* ***********NEW VALUES**********
pos 1 = (0) = Volt,
pos 2 = (1) = Spare, 
pos 3 = (2) = Counter, 
pos 4 = (3) = time-clock,
pos 5 = (4) = RPM, 
pos 6 = (5) = not used
pos 7 = (6) = not used
pos 8 = (6) = not used
*/ 


//**************



int numeral[10] = {
//int numeral[][10] = {
  0b00000000, //0
  0b00000001, //1
  0b00000010, //2
  0b00000011, //3
  0b00000100, //4
  0b00000101, //5
  0b00000110, //6
  0b00000111, //7
  0b00001000, //8
  0b00001001, //9
};
//#include <TimeLib.h>
//#include <Adafruit_Sensor.h>


//*******************************************************************************
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
//********************************************************************************



int ones;
int Temp;
int value=0;
int value1=0;
int lastValue =0;
int temp1, temp2, temp4;// storage for rotary sw binaries
int ledState = LOW;             // ledState used to set the LED
int RotaryA = 11;  // rotary bit A
int RotaryB = 12; // rotary bit B
int RotaryC = 16; // rotary bit C
int Number_Value = 0;    
int ledPin1 = 13;// Sensor led ( LED 1)
//int sensorValue = analogRead(A1);// volt POT
//int inp = A1; // 0-5 v analog in POT
int d1 = 8; // digit 1 of 3,, 1's NIXIE
int d2 = 7; // digit 2 of 3,, 10's NIXIE
int d3 = 6; // digit 3 of 3,, 100's NIXIE
int d4 = 9; // digit 4  extended NIXIE
const int U2_7441pins[] = {10, 3, 4, 5};
const int Nixie_Digit = 4; // Number of digits in the display // was 4
const int digitPins[Nixie_Digit] = {9, 6, 7, 8}; // DIGIT 4,3,2,1 1000'S 100'S 10'S 1'S
const long interval = 100;  // interval at which to blink (milliseconds)

#define INTERRUPT_PIN 0   // Arduino Mega digital pin 2
volatile int interruptCount;
int rpm = 0;// int removes decimal float // was FLOAT now int
int numpoles = 60;// 30 give CLOSE RPM accuracy // was FLOAT now int

int RPM_Sw_Enable = 17;  // i/o 17 activate
void setup()
{
 
//   setTime(3, 10, 30, 24, 5, 2021);// set up clock time
 //Serial.begin(300, SERIAL_7N1);
  Serial.begin(9600);
  pinMode(10, OUTPUT); // A
  pinMode(3, OUTPUT); // B
  pinMode(4, OUTPUT); // C
  pinMode(5, OUTPUT); // D
  pinMode(d1, OUTPUT); // digit 1
  pinMode(d2, OUTPUT); // digit 2
  pinMode(d3, OUTPUT); // digit 3
  pinMode(d4, OUTPUT); // digit 4 aka extended // not used here
  pinMode(ledPin1, OUTPUT);// i/o 13 home led
  pinMode(RotaryA, INPUT_PULLUP); // sw3 C
  pinMode(RotaryB, INPUT_PULLUP); // sw2 B
  pinMode(RotaryC, INPUT_PULLUP); // sw1 A

 // pinMode(INTERRUPT_PIN, INPUT_PULLUP);//  i/o 0
   pinMode(INTERRUPT_PIN, INPUT);//  i/o 0
 // attachInterrupt(INTERRUPT_PIN, interruptFired, CHANGE);
 // pinMode(RPM_Sw_Enable, OUTPUT); // RPM sensor enable
 // digitalWrite(RPM_Sw_Enable, LOW);
  }

void loop(){
 // read BCD sw ( rotary)
  if (digitalRead(RotaryA) == 0) { temp1 = 1;} else { temp1 = 0;}//if this is LOW it will set the bit to the number it should be
  if (digitalRead(RotaryB) == 0) { temp2 = 2;} else { temp2 = 0;}
  if (digitalRead(RotaryC) == 0) { temp4 = 4;} else { temp4 = 0;}
 //delay (10);
  ones = temp1 + temp2 + temp4 ; // add them all up,=  0-7 value
  Number_Value = ones;
    for (byte i = 0; i < 4; i++) {//set up the 4 bits to the 7441
    byte temp = bitRead(ones, i);
    digitalWrite(U2_7441pins[i], temp); // load switch value to 7441 for displaying in digit 
   
      }
 
  // blink display once when rotary sw turned
  if (Number_Value != lastValue) {
    digitalWrite(ledPin1, LOW);// flash i/o 13 ( sensor LED)( LED 1)
    digitalWrite(d1, 1);// flash NIXIE tube digit 1
    digitalWrite(d2, 0);// turn OFF digit 2
    digitalWrite(d3, 0);// turn OFF digit 3
    delay(300);// was 500
    digitalWrite(ledPin1, HIGH);// led 1
    digitalWrite(d1, LOW);// flash NIXIE tube digit 1
    digitalWrite(d2, 0);// turn OFF digit 2
    digitalWrite(d3, 0);// turn OFF digit 3
      delay(10);
  
     lastValue=Number_Value;
  }

   if (Number_Value == 0) {// if rotary =0 do VOLTMETER (sw pos 1)
        VoltMeter(); 
        Serial.println("    Volt Meter     ");
       Serial.println("   \n\r  ");
     } else {
   if (Number_Value == 1) {// if rotary =1 do BLINKY of NIXIE digit 1 (sw pos 2)AKA SPARE
       Blinky(Number_Value); //
        Serial.println("     Blinky     ");
        Serial.println("   \n\r   ");
      } else {
   if (Number_Value == 2) {// if rotary =2 do 3 digit counting ( sw pos 3)
      Disp_cntr();
       Serial.println("     Count UP    ");
       Serial.println("    \n\r    ");
      } else {
   if (Number_Value == 4) {// if rotary =4 do RPM ( sw pos 5)
       RPM();
        Serial.println("    RPM    ");
       Serial.println("    \n\r    ");
      } else {
   if (Number_Value == 3) {// if rotary =3 do time clock ( sw pos 4)
       RPM();
        Serial.println("     CLOCK time    ");
       Serial.println("    \n\r    ");
       return;
   }
    } 
     }
      }
       }
        }
    
void showNumber( int number)
{
   if (number == 0) {
    showDigit( 0, Nixie_Digit - 1) ; // Display 0 in the rightmost digit, blank the other 2 digits
  } else {
  
    // Display the value corresponding to each digit
    // leftmost digit is 0, rightmost is one less than the number of places
    for ( int digit = Nixie_Digit - 1; digit >= 0; digit--)  {
      if (number > 0)  {
        showDigit( number % 10, digit) ;
        number = number / 10;
        
        Serial.println("     Count UP    ");    
   }
    }
     }
      }

// Do display the value
void showDigit( int ones, int digit)
{
    digitalWrite( digitPins[digit], HIGH );
    for (byte i = 0; i < 4; i++) {//set up the 4 bits to the 7441
     boolean isBitSet = bitRead(numeral[ones], i);
     digitalWrite( U2_7441pins[i], isBitSet);// load switch value to 7441 for displaying in digit 
  }
  delay(5);
  digitalWrite( digitPins[digit], LOW );
  }

void  RPM() // sw = 4  RPM aka ( sw in 5th ( last ) position)
{
    temp1 = digitalRead(RotaryA);
      temp2 = digitalRead(RotaryB);
       temp4 = digitalRead(RotaryC);
     if (temp1 == HIGH){// aka sw is HIGH
      if (temp2 == HIGH){// aka sw is HIGH
       if (temp4 != HIGH){// aka sw is LOW
   checkRPM();
    }
     }
      }
       }
void checkRPM() {

   digitalWrite(RPM_Sw_Enable, HIGH);// turn ON RPM sensor (i/o 17)
        noInterrupts() ;
        interruptCount = 0;  // set variable in critical section
        interrupts() ;
        delay (200);// was 100
        noInterrupts() ;
        int critical_rpm = interruptCount ;  // read variable in critical section 
        interrupts() ;
        rpm = ((critical_rpm)*(60))/(numpoles)*10;
        Serial.println(" R P M   ");
        Serial.println(rpm);
       // Serial.println("   \n   ");
        delay (100);// was 100
       //  Serial.println("                  ");
      // Serial.print("\n\n");
        //Serial.print('\n');
         //Serial.print('\t');// tab
          Serial.print("BIN");
      }
  
    void interruptFired()
    {
    interruptCount++;
     showNumber(rpm);
    }

void Disp_cntr()// sw = 2 999 COUNTER ( sw 3rd position)
{
  for (value=0; value < 1000; value++) {// count to 999
     temp1 = digitalRead(RotaryA);
      temp2 = digitalRead(RotaryB);
       temp4 = digitalRead(RotaryC);
     if (temp1 == HIGH){// aka sw is HIGH
      if (temp2 != HIGH){// aka sw is LOW
       if (temp4 == HIGH){// aka sw is HIGH
      unsigned long startTime = millis();
    for (unsigned long elapsed=0; elapsed < 8; elapsed = millis() - startTime) {// was 600
      showNumber(value);// displays incrementing 2 digits on NIXIE
        Serial.println("     Blinky     ");
    }
     }
      }
       }
        }
        }

void VoltMeter()// sw = 0 aka (sw in 1st position)
{


    int value1 = analogRead(A1); // Read analog sensor @ A1
 //int sensorValue = analogRead(A1);// volt POT ( 0-999 full scale)( 0v to 5v)
 // digitalWrite(RPM_Sw_Enable, LOW);// turn OFF RPM sensor (i/o 17)
    temp1 = digitalRead(RotaryA);
      temp2 = digitalRead(RotaryB);
       temp4 = digitalRead(RotaryC);
     if (temp1 == HIGH){// aka sw is HIGH
      if (temp2 == HIGH){// aka sw is HIGH
       if (temp4 == HIGH){// aka sw is HIGH
      
  // value = analogRead(A1); // Read analog sensor @ A1
   float voltage = value1 *(999.0 / 1023.0);// was 5
  showNumber(value1);
   Serial.println("    Volt Meter     ");

   }
      }}}
void Blinky (int digit) // sw = 1 aka ( sw in 2nd postion)
{
   temp1 = digitalRead(RotaryA);
      temp2 = digitalRead(RotaryB);
       temp4 = digitalRead(RotaryC);
     if (temp1 != HIGH){// aka sw is 0
      if (temp2 == HIGH){// aka sw is 1
       if (temp4 == HIGH){// aka sw is 1
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    // set the LED with the ledState of the variable:
     digitalWrite(ledPin1, ledState);// blink the SENSOR  led 1
       digitalWrite(d2, ledState);
         digitalWrite(d1, !ledState);
  }
   }
    }
     }
      }
