/* push button usage as interrupt timer for idle mode as interrupt button updates a counter, that runs a switchcase. each feature their own segment */ #define F_CPU 8000000UL //8mhz Internal -U lfuse:w:0xe4:m -U hfuse:w:0x99:m #include #include const int numRows = 2; const int numCols = 16; const byte BUTTON = 25; volatile int var = 0; const byte voltage = 30; const byte beePin = 29; const byte resistor = 28; const byte gatevcc = 27; const byte ReadPin = 26; const byte AmpSig = 24; const byte Discharge = 4; const byte Charge = 3; const int squackPin = 12; LiquidCrystal lcd(18, 19, 20,21,22,23); int R2 = 1000000; int R1 = 5000000; int R3 = 190000; int VIN = 5; int mVperAmp = 185; double Voltage = 0; double VRMS = 0; double AmpsRMS = 0; /* Interrupt Service Routine (ISR) void switchPressed () { if (digitalRead (BUTTON) == HIGH) var++; //300 second timer for sleep, } // end of switchPressed*/ /*//void snore(int timer) void snore() { lcd.display(); lcd.clear(); lcd.setCursor(0,0); // attachInterrupt (digitalPinToInterrupt (BUTTON), switchPressed, CHANGE); // attach interrupt handler lcd.print("Sleep in 10"); }*/ float getVPP(int sensorIn) { float result; int readValue; //value read from the sensor int maxValue = 0; // store max value here int minValue = 1024; // store min value here uint32_t start_time = millis(); while((millis()-start_time) < 250) //sample for 0.25 Sec { readValue = analogRead(sensorIn); // see if you have a new maxValue if (readValue > maxValue) { /*record the maximum sensor value*/ maxValue = readValue; } if (readValue < minValue) { /*record the maximum sensor value*/ minValue = readValue; } } // Subtract min from max result = ((maxValue - minValue) * 5.0)/1024.0; return result; } void shortc() { lcd.display(); lcd.clear(); lcd.setCursor(0,0); lcd.print("BEEP"); pinMode(beePin, INPUT ); analogWrite(squackPin,0); digitalWrite(gatevcc,HIGH); /*while (analogRead(beePin) == LOW) { analogWrite(squackPin,50); } */ lcd.setCursor(0,1); lcd.print(analogRead(beePin)); delay(100); analogWrite(squackPin,0); } void res() { lcd.display(); lcd.clear(); lcd.setCursor(0,0); lcd.print("Resistor"); // read the input voltage on the resistance terminals int resValue = getVPP(resistor); // convert the reading to voltage. //float resVolt = resValue * (5.0 / 1023.0); float Resistor = R3*(1/((VIN-resValue)-1)); lcd.setCursor(0,1); lcd.print(Resistor); lcd.print(" Ohm"); delay(100); } void capacitor() { lcd.display(); lcd.clear(); lcd.setCursor(0,0); lcd.print("Capacitor"); lcd.setCursor(0,1); lcd.print(analogRead(ReadPin)); delay(100); } void current() { lcd.display(); lcd.clear(); lcd.setCursor(0,0); lcd.print("Current"); Voltage = getVPP(AmpSig); VRMS = (Voltage/2.0) *0.707; AmpsRMS = (VRMS * 1000)/mVperAmp; lcd.setCursor(0,1); lcd.print(AmpsRMS); lcd.print(" Amps RMS"); delay(100); } void volt() { lcd.display(); lcd.clear(); lcd.setCursor(0,0); lcd.print("Voltage "); lcd.setCursor(0,1); int voltValue = getVPP(voltage); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): //float Vout = voltValue * (5.0 / 1023.0); float Vin = (R2/(R1+R2))/voltValue; lcd.setCursor(0,1); lcd.print("Vin "); lcd.print(Vin); lcd.print(" V"); delay(100); } void setup () { lcd.begin(numCols, numRows); pinMode(BUTTON, INPUT); pinMode(AmpSig,INPUT); pinMode(ReadPin, INPUT); pinMode(resistor, INPUT); pinMode(voltage, INPUT); pinMode(beePin, INPUT); pinMode(squackPin, OUTPUT); pinMode(gatevcc, OUTPUT); //Serial.begin(9600); lcd.print("FabLab Oulu"); lcd.setCursor(0,1); lcd.print("Mikko Toivonen"); delay(1000); } // end of setup void loop () { if (digitalRead (BUTTON) == LOW) { var++; } //lcd.display(); delay(175); //lcd.print(var); switch (var) { case 1: shortc();//short circuit checking break; case 2: volt();// voltage measurement break; case 3: res();// resistor measurement break; case 4: current();// current measurement break; case 5: capacitor();//capacitor measurement /* break; case 6: snore();//putting the chip to sleep break; */ default: { lcd.display(); lcd.clear(); lcd.setCursor(0,0); lcd.print("Press Button"); var = 0; } //10 second timer for sleep break; } }