The 1000 foot view of this project is to create a running wheel for mice, with the ability to detect, store, display revolutions through a hall effect sensor, as well as turn the arduino into "sleep mode" for periods of prolonged inactivity (mice run prolifically throughout the night, little to none during the day). I've put together a sketch of what this looks like (first attached image).
My friend and I have put together a prototype for this project using an arduino nano. I'm attaching the code we currently have (proper coding is where we are really lacking):
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
volatile byte half_revolutions;
volatile byte hooyah;
void magnet_detect()//This function is called whenever a magnet/interrupt is detected by the arduino
{
half_revolutions++;
Serial.println(half_revolutions);
hooyah=half_revolutions;
}
void setup() {
Serial.begin(9600);
attachInterrupt(0, magnet_detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
//half_revolutions=0;
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void loop() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print(hooyah);
display.display(); // actually display all of the above
}
At present, we have a code that can detect and display the hall effect sensor. What we really need now is:
ability to display remaining battery power on the OLED
ability for the arduino to go into sleep/power saving mode after ~20 seconds of no hall effect detection (we need these to be able to run on a 9v battery for >14 days)
I've attached images for the arduino and model we have currently (2-4th attached image)
Other ideas that I thought could improve the model, but i'm unsure of their feasibility:
If different battery power source would be better than the 9v (i.e. lithium, etc). The wheel needs to be wireless.
If there is a way to display revolutions through a remote (IR or other). What I really need is to collect the revolution count at distinct times throughout the day, so a continuous display of the revolutions on the OLED isn't beneficial, as "turning on" the display to show revolutions when I check it would be.
TheMemberFormerlyKnownAsAWOL:
Don't do serial I/O in interrupt context
I'm pretty sure there rules are clear on this.
Thanks for the suggestion, but I'll be brutally honest, I don't exactly know what you mean. What is the alternative? Do you have any coding examples for what should be used instead of the interrupt I'm currently using? I'm all ears, as my coding ability is currently very novice.
I'm not quite sure what your second sentence is implying either. There are clear rules on this? Where? Who wrote them? Why?
Basically if you could flesh out what you're trying to say, I would really appreciate it.
Note that you can't run an Arduino directly on 9V at low power. If you connect 9V to the linear regulator you will be throwing away half the power in your battery just heating the regulator. And for low power you want to use the lowest voltage possible (the ATmega328p goes down to 1.8V, I think).
I think you would be better off running directly off a lithium cell.
johnwasser:
Note that you can't run an Arduino directly on 9V at low power. If you connect 9V to the linear regulator you will be throwing away half the power in your battery just heating the regulator. And for low power you want to use the lowest voltage possible (the ATmega328p goes down to 1.8V, I think).
I think you would be better off running directly off a lithium cell.
Thanks for the helpful article. Definitely a lot to take in there, but it's super helpful.
Going to try to implement some of the sleep code there. So with my current setup (Arduino nano), using a lithium battery is the best play for prolonged life?
I'm a noob so I'll have to ask, exactly what specifications would this battery be? (amazon links greatly appreciated!)
for the battery level you will need to code/calibrate a volt meter using analog read and map,and an actual volt meter for reference, and use a very high resistance voltage divider so there is almost no current, on the battery if its 9V to get your battery full voltage slightly below max range of analog input (which is the operating voltage - around 5V) .
you might want to use a button to display the level not to waste power on oled
you could maybe turn the wheel into a generator to run arduino from using a dc motor, or to charge battery, or for the rpm probe XD no need for sleep then
it would slow the mice a bit
If I used the battery, maybe 5V from a cell phone hooked to the 5V rail, voltage over 6V i think would damage the atmega