Attempting to combine an OLED display sketch and an IR receiver sketch on an Uno R3, so I can verify what codes are being received from an IR transmitter I'm working on.
Both sketches work properly when ran independently. The issue presenting itself is when both sketches are combined without changing any physical configuration, it fails to allocate the OLED display.
OLED display
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#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 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.setCursor(20,0);
display.print("IR Signal Read");
display.display();
}
void loop()
{
}
IR reveiver
#include <SPI.h>
#include <Wire.h>
#include <IRremote.h>
const int RECV_PIN=3;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
irrecv.blink13(true);
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println("Hex:\t\tDecimal:");
Serial.print(results.value, HEX);
Serial.print("\t\t");
Serial.println(results.value, DEC);
dump(&results);
irrecv.resume(); // Receive the next value
}
}
void dump(decode_results *results)
{
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
int count = results->rawlen;
if (results->decode_type == UNKNOWN)
Serial.print("Unknown encoding: ");
else if (results->decode_type == NEC)
Serial.print("Decoded NEC: ");
else if (results->decode_type == SONY)
Serial.print("Decoded SONY: ");
else if (results->decode_type == RC5)
Serial.print("Decoded RC5: ");
else if (results->decode_type == RC6)
Serial.print("Decoded RC6: ");
Serial.print(results->value, HEX);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 1; i < count; i++)
{ //The change is inside this loop. Changed to HEX for easy copy and paste.
Serial.print("");
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
Serial.print(", ");
// Serial.print("\t");
}
Serial.println();
}
Combined
#include <SPI.h>
#include <Wire.h>
#include <IRremote.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#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 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int RECV_PIN=3;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.setCursor(20,0);
display.print("IR Signal Read");
display.display();
irrecv.enableIRIn(); // Start the receiver
irrecv.blink13(true);
}
void loop()
{
if (irrecv.decode(&results))
{
display.clearDisplay();
Serial.println("Hex:\t\tDecimal:");
Serial.print(results.value, HEX);
Serial.print("\t\t");
Serial.println(results.value, DEC);
display.setCursor(20, 0); // Start at top-left corner below yellow
display.print("IR Signal Read");
display.setCursor(0, 26);
display.print("Hex: ");
display.print(results.value, HEX);
display.setCursor(0, 36);
display.print("Dec: ");
display.print(results.value, DEC);
display.display();
dump(&results);
irrecv.resume(); // Receive the next value
}
}
void dump(decode_results *results)
{
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
int count = results->rawlen;
if (results->decode_type == UNKNOWN)
Serial.print("Unknown encoding: ");
else if (results->decode_type == NEC)
Serial.print("Decoded NEC: ");
else if (results->decode_type == SONY)
Serial.print("Decoded SONY: ");
else if (results->decode_type == RC5)
Serial.print("Decoded RC5: ");
else if (results->decode_type == RC6)
Serial.print("Decoded RC6: ");
Serial.print(results->value, HEX);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 1; i < count; i++)
{ //The change is inside this loop. Changed to HEX for easy copy and paste.
Serial.print("");
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
Serial.print(", ");
// Serial.print("\t");
}
Serial.println();
}
What am I missing?