Button 1 on the remote generates 4 in the sketch. Button 2 generates 5.
I'm reluctant to post the complete sketch because it is a work-in-progress but here goes...
/*
February, 2024
This sketch will identify the position of the switch.
The switch position will identifywhether that a 2, 3 and 4 cell
LIPO battery is connected. When the battery is depleted to 3.2 volts
per cell, it will turn off the LED and power down.
NOTE: Some Nano's require Tools>>Processor ATmega328P (Old Bootloader)
*/
#include <LowPower.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "IRremote.hpp"
#define IR_RECEIVE_PIN 9 //IR Receiver
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // 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 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declare oled object
const int switchOne = 3; // digital pin Position 1
const int switchTwo = 4; // digital pin Position 3
// const int ledPin = LED_BUILTIN;
const int ledPin = 12; //Pin connected to the LED that identifies the switch position
const int wakeUpPin = 2;
int oneState = 0; //Initialize current state
int threeState = 0;
int pos1Loops = 0; //Initialize the loop count for each position
int pos2Loops = 0;
int pos3Loops = 0;
const int voltageSensor = A0; //Pin connected to voltage regulator
float vOUT = 0.0; //
float vIN = 0.0; //
float R1 = 30000.0; //Voltage Regulator resistor 1
float R2 = 7500.0; //Voltage Regulator resistor 2
int value = 0; //Value read from voltage pin
const float voltage_divider = 3.9; //Adjusted. Not 5.0 as in examples
String position_identifier = "Initialized";
//Cut off voltage depending on battery size
const float pos1CutoffVoltage = 6.4; //2 cell battery
const float pos2CutoffVoltage = 9.6; //3 cell battery
const float pos3CutoffVoltage = 12.8; //4 cell battery
bool ledIsOn = false;
void wakeUp() {
// delay(5000);
digitalWrite(12, HIGH);
Serial.println("In the wakeUp function");
// delay(1000);
}
void setup() {
Serial.begin(9600); //Print output
pinMode(switchOne, INPUT_PULLUP); //switchOne digital mode
pinMode(switchTwo, INPUT_PULLUP); //switchTwo digital mode
pinMode(ledPin, OUTPUT);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); //IR Receiver
pinMode(wakeUpPin, INPUT_PULLUP);
}
void loop() {
// delay(5000);
// // attachInterrupt(2, wakeUp, LOW);
// attachInterrupt(digitalPinToInterrupt(2), wakeUp, CHANGE);
// // LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
// attachInterrupt(digitalPinToInterrupt(wakeUpPin), wakeUp, LOW); //LOW, CHANGE, RISING, FALLING
// LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); //Power down
// detachInterrupt(wakeUpPin);
// delay(1000);
oneState = digitalRead(switchOne);
threeState = digitalRead(switchTwo);
value = analogRead(voltageSensor);
//vOUT = (value * 5.0) / 1024.0;
vOUT = (value * voltage_divider) / 1024.0;
vIN = vOUT / (R2 / (R1 + R2));
//////// TESTING ////////////
vIN = 13; //TEST 6.3, 9.5, 12.7 //
// Serial.print("value = ");
// Serial.println(value);
// Serial.print("vOUT = ");
// Serial.println(vOUT);
// Serial.print("vIN = ");
// Serial.println(vIN);
///////////////////////////////////
if (oneState == LOW) {
Serial.println("Position 1");
while (pos1Loops < 4) {
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
pos1Loops++;
}
threeState = LOW;
if (vIN < pos1CutoffVoltage) {
KillLED(vIN);
} else {
digitalWrite(ledPin, HIGH);
position_identifier = "2 Cell Battery";
}
} else if (threeState == LOW) {
Serial.println("Position 3");
while (pos3Loops < 4) {
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(500);
pos3Loops++;
}
oneState == LOW;
if (vIN < pos3CutoffVoltage) {
KillLED(vIN);
} else {
digitalWrite(ledPin, HIGH);
position_identifier = "4 Cell Battery";
}
} else {
Serial.println("Position 2");
while (pos2Loops < 4) {
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(500);
pos2Loops++;
}
oneState == LOW;
if (vIN < pos2CutoffVoltage) {
KillLED(vIN);
} else {
digitalWrite(ledPin, HIGH);
position_identifier = "3 Cell Battery";
}
}
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(15, 0);
oled.println(position_identifier);
oled.setTextSize(2);
oled.setCursor(0, 15); // position to display
oled.print(vIN); // text to display
oled.println(" Volts");
oled.display();
delay(1000);
/////////// IR Receiver /////////////
if (IrReceiver.decode()) {
uint16_t address = IrReceiver.decodedIRData.command;
if (address == 4) {
Serial.println(address);
delay(1000);
attachInterrupt(digitalPinToInterrupt(wakeUpPin), wakeUp, LOW); //LOW, CHANGE, RISING, FALLING
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); //Power down
detachInterrupt(wakeUpPin);
delay(1000);
// digitalWrite(12, LOW);
// KillLED(0);
} //else if (address == 5) {
// wakeUp();
// Serial.println(address);
// digitalWrite(12, HIGH);
// }
}
IrReceiver.resume();
}
void KillLED(float vIN) {
Serial.print("In KillLED fuction."); // Voltage =
Serial.println(vIN);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(20, 0);
oled.println("Powered Down");
oled.setTextSize(2);
oled.setCursor(0, 15); // position to display
oled.print(vIN); // text to display
oled.println(" Volts");
oled.display();
// delay(5000);
// attachInterrupt(2, wakeUp, LOW);
attachInterrupt(digitalPinToInterrupt(2), wakeUp, LOW); //LOW, CHANGE, RISING, FALLING
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); //Power down
}
attachInterrupt(digitalPinToInterrupt(wakeUpPin), wakeUp, LOW);
When is "LOW" the TX LED on the Arduno Uno pulsates and the 5v LED on pin 12 remains constantly lighted. This is the desired behavior..
When the remote sends a "4" indicating the number one button on the remote was pressed, the TX LED no longer pulsates. It remains. But at the same time, the wakeUP() function was called printing out "In the wakeUp function" very rapidly. Pressing the number one button on the remote again has no affect.
What do I need to do to wake up the Uno?
I tried to limit the code by my original post using the LowPower library example but the whole sketch is in this post.
Thank you for your time and opinion.