//Developed in a day by Rob Smith 2014. Distributed under GNU public license, share this as you please and I don't care about credit

//Version 1.2
//Tested to work only a simulator and not on a real Arduino, however it should work just fine.
//LCD is for a 16x2 using the standard LiquidCrystal displays (LCDs) based on the Hitachi HD44780 (or a compatible) chipset, which is found on most text-based LCDs.
//Ohm measurment circuit is designed to run from the +5v to a 50 ohm resistor to analog input 1 then to the atomizer then to the arduino ground. Never should this circuit be active during firing of the atomizer DC-DC converter.
//Use a MOSFET or transistor to turn this circuit on and off using the ohmPin.
//Sleep functions should now function at a rest timer of 5 seconds.
//Displays Battery voltage and atomizer resistance when the fire button is not depressed and displays atomizer voltage, wattage, and amperage used when fire button is depressed.
// Added Libraries
#include <LiquidCrystal.h>
#include <avr/sleep.h>
// set pin numbers:
const int firebuttonPin = 7; // Connected to fire switch on the Box from pin 2
const int ohmPin = 8; // Sets the pin used to enable the OHM meter circuit to digital output 8
const int powerConverter = 6; // Connected to inhibit/enable/on-off of the DC DC Converter connected to pin 4
const int ledPin = 13; // Internal LED to verifyt he circuit is activating for trouble shooting connected to pin 13
const int analogPin = 0; // potentiometer wiper (middle terminal) connected to analog pin 0, outside leads to ground and +5V
const int atomizerVoltage = 1; // declares the analog pin 1 as the voltage output of the DC-DC converter to be read
const int batteryVoltage = 2; // declares the analog pin 2 as the voltage output of the battery pack
LiquidCrystal lcd(12, 11, 5, 4, 3, 9);
int wakePin = 2; // pin used for waking up
// Variables
int buttonPushCounter = 0; // Counts the number of button pushes
int buttonState = 0; // State of Fire Button
int ohmpowerState = 0; // State of the Ohm circuit power signal on-off
int lastButtonState = 0; // Last state of the push button
int raw = 0; // variable to store the raw input value
int Vin = 5; // variable to store the input voltage controlled 5v supply from the arduino 5v pin output
float Vout = 0; // variable to store the output voltage the voltage being read after it passes through the atomizer coil
float R1 = 50; // variable to store the R1 value which is a 50 ohm resistor
float R2 = 0; // variable to store the R2 value which is the resistance of the atomizer coil
float buffer = 0; // buffer variable for calculation
float Vbatt = 0; // Assigns a variable for battery voltage
float Vatty = 0; // Assigns a variable for atomizer voltage
int sleepStatus = 0; // variable to store a request for sleep
int count = 0; // counter
void wakeUpNow() // here the interrupt is handled after wakeup
{ // Wake up action
digitalWrite (powerConverter, LOW); // Keep the DC-DC converter output turned off
}
void setup() { // put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16, 2); // set up the LCD's number of columns and rows
pinMode (firebuttonPin, INPUT); // Set pin 7 as fire button input
pinMode (ledPin, OUTPUT); // Set Pin 13 as LED output
pinMode (ohmPin, OUTPUT); // Set Pin 8 as the FET G output on pin 8 to enable the OHM meter
pinMode (powerConverter, OUTPUT); // Set Pin 6 as DC-DC converter output
pinMode(wakePin, INPUT);
//attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function wakeUpNow when pin 2 gets LOW
}
void sleepNow() { // here we put the arduino to sleep
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register so sleep is possible. just a safety pin
attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function wakeUpNow when pin 2 gets LOW
sleep_mode(); // here the device is actually put to sleep!! THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // first thing after waking from sleep is disable sleep.
detachInterrupt(0); // disables interrupt 0 on pin 2 so the wakeUpNow code will not be executed during normal running time.
}
void loop() { // put your main code here, to run repeatedly:
// Fire button code
buttonState = digitalRead (firebuttonPin); // Monitor the Fire button for closed circuit
if (buttonState == HIGH) { // When fire button is closed and ohm circuit is turned off
digitalWrite (ohmPin, LOW); // Disables the Ohm Meter Power circuit
delay (100); // Delay the fire function to prevent ohm circuit and fire function being active at the same time
digitalWrite (ledPin, HIGH); // Turn on the ledpin
digitalWrite (powerConverter, HIGH); // Turn on the DC-DC converter output
// Voltage to atomizer monitoring code
Vatty = analogRead(atomizerVoltage); // Reads the voltage of analog pin 1 to determin the voltage going to the atomizer
float Avoltage = Vatty * (5.0 / 1023); // Convert the raw value being read from analog pin 1 into a voltage value
// Display the output power of the DC-DC converter in Watts
Vatty = analogRead(atomizerVoltage); // Reads the voltage of analog pin 1 to determin the voltage going to the atomizer
int Watts = (Avoltage * Avoltage) / R2; // Convert the power going the atty to watts using voltage squared divided by resistance formula using the value of resistance found above in ohm meter
// Display the output current of the DC-DC converter in Amps
Vatty = analogRead(atomizerVoltage); // Reads the voltage of analog pin 1 to determin the voltage going to the atomizer
float Amps = Avoltage / R2; // Convert the power going the atty to Amps using voltage divided by resistance formula using the value of resistance found above in ohm meter
// Display meter ouptuts
lcd.clear();
lcd.home();
lcd.print("Atty volts: "); // to display the voltage on the LCD
lcd.print(Avoltage);
lcd.setCursor(0, 1);
lcd.print("W: "); // displays the wattage on the LCD
lcd.print(Watts);
lcd.setCursor(7, 1);
lcd.print("A: "); // displays the wattage on the LCD
lcd.print(Amps);
// Set the count timer to zero to reset the 5 second idle sleep function
count=0;
}
buttonState = digitalRead (firebuttonPin); // Monitor the Fire button for closed circuit
if (buttonState == LOW) { // Monitor the Fire button if it is anything besides Open
digitalWrite (ohmPin, HIGH);
digitalWrite (ledPin, LOW); // Keep the led turned off
digitalWrite (powerConverter, LOW); // Keep the DC-DC converter output turned off
// Votlage remaining in battery code
Vbatt = analogRead(batteryVoltage); // Reads the voltage of analog pin 2 to determin the voltage going to the battery
float Bvoltage = Vbatt * (5.0 / 1023); // Convert the raw value being read from analog pin 1 into a voltage value
int Bpercent = (Bvoltage - 3.2) * 100;
// Ohm Meter code
ohmpowerState = digitalRead (ohmPin);
if (ohmpowerState == HIGH) {
raw = analogRead(analogPin); // Reads the Input PIN, this circuit is set to a on-off switch to cut the 5v supply voltage unless depressed in order to keep the Arduino isolated from the DC-DC converter output voltage to prevent damage to Arduino
Vout = (5.0 / 1023.0) * raw; // Calculates the Voltage on th Input PIN by dividing the max voltage (5) by the number of steps the Arduino can handle (1023) and then multiplies the resulting increment by the raw input value from analogRead pin
buffer = (Vin / Vout) - 1; // Creates a variable requried for the math to get ohms instead of volts by dividing Vin (5) by Vout from the argument above and subtracting 1
R2 = R1 / buffer; // Divides R1 (10 ohms) by the variable created above (buffer), the result is the resistance of R2 (atmoizer)
// Display meter outputs
lcd.clear();
lcd.home();
lcd.print("Battery %: "); // display the voltage on the LCD
lcd.print(Bpercent);
lcd.setCursor(0, 1);
lcd.print("Ohms: "); // displays the resistance in ohms on the screen
lcd.print(R2); // displays the resistance in ohms on the screen
// display information about the counter
Serial.print("Awake for ");
Serial.print(count);
Serial.println("sec");
count++;
delay(1000); // waits for a second
// check if it should go to sleep because of time
{
if (count >= 5) {
lcd.clear();
lcd.print("Entering Sleep");
delay(100); // this delay is needed, the sleep function will provoke a Serial error otherwise!!
count = 0;
sleepNow(); // sleep function called here
}
}
// compute the serial input
// if (Serial.available()) {
// int val = Serial.read();
// if (val == 'S') {
// Serial.println("Serial: Entering Sleep mode");
// delay(100); // this delay is needed, the sleep function will provoke a Serial error otherwise!!
// count = 0;
// sleepNow(); // sleep function called here
// }
// if (val == 'A') {
// Serial.println("No u"); // classic dummy message
}
}
}
Edit: Version 1.2 has cleaned up code, functional sleep and recover from sleep state. Pin 2 and Pin 7 must be connected to the same switch to trigger a transistor to set the normally high pin 2 connected to arduino's RX pin to low when Pin 7 goes high (switch closes) otherwise once it goes to sleep you cannot wake it!