Trying to use a basic if statement to select between two images based on the position of a switch for a model rocket system. No matter how I go about it, The screen flashes both results on the screen.
All i want is for it to say "Armed" when the switch is on, and "Disarmed" when it is off. Using Bitmap images. Code is made with the Adafruit library example
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <JC_Button.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET 4
#define TEXT_SIZE 4
// 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 4 // 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);
#define LOGO_HEIGHT 32
#define LOGO_WIDTH 128
static const unsigned char PROGMEM disarmed[] =
{ 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x0f, 0xe0, 0x01, 0xc0, 0x0f, 0xfe, 0x1f, 0xfe, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x1f, 0xf8, 0x01, 0xc0, 0x0f, 0xfe, 0x1f, 0xfe, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x3c, 0x38, 0x03, 0xe0, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x70, 0x08, 0x03, 0xe0, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x70, 0x00, 0x07, 0xf0, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x70, 0x00, 0x07, 0x70, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x78, 0x00, 0x07, 0x70, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x7f, 0x00, 0x0f, 0x38, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x3f, 0xe0, 0x0e, 0x38, 0x0f, 0xfc, 0x1f, 0xfc, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x0f, 0xf8, 0x0e, 0x38, 0x0f, 0xfc, 0x1f, 0xfc, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x01, 0xf8, 0x1c, 0x1c, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x3c, 0x1c, 0x1c, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x1c, 0x3f, 0xfe, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x1c, 0x3f, 0xfe, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x1c, 0x38, 0x0e, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x40, 0x1c, 0x78, 0x0f, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x70, 0x78, 0x70, 0x07, 0x0e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x7f, 0xf0, 0x70, 0x07, 0x0e, 0x00, 0x1f, 0xfe, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x1f, 0xc0, 0xe0, 0x03, 0x8e, 0x00, 0x1f, 0xfe, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
};
static const unsigned char PROGMEM armed[] =
{ 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0xe0, 0x07, 0xfc, 0x01, 0xf8, 0x3f, 0x07, 0xff, 0x87, 0xfe, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0xe0, 0x07, 0xff, 0x01, 0xf8, 0x3f, 0x07, 0xff, 0x87, 0xff, 0x80, 0x01, 0xff,
0xff, 0x80, 0x01, 0xf0, 0x07, 0x07, 0x01, 0xf8, 0x3f, 0x07, 0x00, 0x07, 0x03, 0xe0, 0x01, 0xff,
0xff, 0x80, 0x01, 0xf0, 0x07, 0x03, 0x81, 0xfc, 0x7f, 0x07, 0x00, 0x07, 0x00, 0xf0, 0x01, 0xff,
0xff, 0x80, 0x03, 0xf8, 0x07, 0x03, 0x81, 0xdc, 0x77, 0x07, 0x00, 0x07, 0x00, 0x70, 0x01, 0xff,
0xff, 0x80, 0x03, 0xb8, 0x07, 0x03, 0x81, 0xdc, 0x77, 0x07, 0x00, 0x07, 0x00, 0x70, 0x01, 0xff,
0xff, 0x80, 0x03, 0xb8, 0x07, 0x03, 0x81, 0xde, 0xf7, 0x07, 0x00, 0x07, 0x00, 0x38, 0x01, 0xff,
0xff, 0x80, 0x07, 0x9c, 0x07, 0x03, 0x81, 0xce, 0xe7, 0x07, 0x00, 0x07, 0x00, 0x38, 0x01, 0xff,
0xff, 0x80, 0x07, 0x1c, 0x07, 0x07, 0x01, 0xce, 0xe7, 0x07, 0xff, 0x07, 0x00, 0x38, 0x01, 0xff,
0xff, 0x80, 0x07, 0x1c, 0x07, 0xfe, 0x01, 0xcf, 0xe7, 0x07, 0xff, 0x07, 0x00, 0x38, 0x01, 0xff,
0xff, 0x80, 0x0e, 0x0e, 0x07, 0xfc, 0x01, 0xc7, 0xc7, 0x07, 0x00, 0x07, 0x00, 0x38, 0x01, 0xff,
0xff, 0x80, 0x0e, 0x0e, 0x07, 0x0e, 0x01, 0xc7, 0xc7, 0x07, 0x00, 0x07, 0x00, 0x38, 0x01, 0xff,
0xff, 0x80, 0x1f, 0xff, 0x07, 0x07, 0x01, 0xc3, 0xc7, 0x07, 0x00, 0x07, 0x00, 0x38, 0x01, 0xff,
0xff, 0x80, 0x1f, 0xff, 0x07, 0x07, 0x81, 0xc3, 0x87, 0x07, 0x00, 0x07, 0x00, 0x70, 0x01, 0xff,
0xff, 0x80, 0x1c, 0x07, 0x07, 0x03, 0x81, 0xc3, 0x87, 0x07, 0x00, 0x07, 0x00, 0x70, 0x01, 0xff,
0xff, 0x80, 0x3c, 0x07, 0x87, 0x03, 0xc1, 0xc0, 0x07, 0x07, 0x00, 0x07, 0x00, 0xe0, 0x01, 0xff,
0xff, 0x80, 0x38, 0x03, 0x87, 0x01, 0xe1, 0xc0, 0x07, 0x07, 0x00, 0x07, 0x03, 0xe0, 0x01, 0xff,
0xff, 0x80, 0x38, 0x03, 0x87, 0x00, 0xe1, 0xc0, 0x07, 0x07, 0xff, 0x87, 0xff, 0x80, 0x01, 0xff,
0xff, 0x80, 0x70, 0x01, 0xc7, 0x00, 0xf1, 0xc0, 0x07, 0x07, 0xff, 0x87, 0xfe, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
};
//const int TriggerPin = 1;
int Safety = 2;
//const int LPins[] = {3,4,5};
void setup() {
Serial.begin(9600);
pinMode(Safety, INPUT);
// 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
}
display.display();
delay(2000); // Pause for 2 seconds
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
//pinMode(Safety, INPUT_PULLUP);
// Clear the buffer
display.clearDisplay();
testdrawtriangle(); // Draw triangles (outlines)
testfilltriangle(); // Draw triangles (filled)
armstatus();
// Invert and restore display, pausing in-between
display.invertDisplay(true);
delay(1000);
display.invertDisplay(false);
delay(1000);
}
// Show the arm status on screen. You MUST call display() after
// drawing commands to make them visible on screen!
///display.drawBitmap(
///(display.width() - LOGO_WIDTH ) / 2,
///(display.height() - LOGO_HEIGHT) / 2,
///disarmed, LOGO_WIDTH, LOGO_HEIGHT, 1);
///display.display();
///delay(1000);
void testdrawtriangle(void) {
display.clearDisplay();
for(int16_t i=0; i<max(display.width(),display.height())/2; i+=5) {
display.drawTriangle(
display.width()/2 , display.height()/2-i,
display.width()/2-i, display.height()/2+i,
display.width()/2+i, display.height()/2+i, SSD1306_WHITE);
display.display();
delay(1);
}
delay(2000);
}
void testfilltriangle(void) {
display.clearDisplay();
for(int16_t i=max(display.width(),display.height())/2; i>0; i-=5) {
// The INVERSE color is used so triangles alternate white/black
display.fillTriangle(
display.width()/2 , display.height()/2-i,
display.width()/2-i, display.height()/2+i,
display.width()/2+i, display.height()/2+i, SSD1306_INVERSE);
display.display();
display.clearDisplay();
delay(1);
}
delay(1000);
}
void armstatus(void) {
//display.clearDisplay();
display.drawBitmap(
(display.width() - LOGO_WIDTH ) / 2,
(display.height() - LOGO_HEIGHT) / 2,
disarmed, LOGO_WIDTH, LOGO_HEIGHT, 1);
delay(1);
}
void loop()
{
if ( digitalRead(Safety) == LOW)
{
display.clearDisplay();
display.drawBitmap(
(display.width() - LOGO_WIDTH ) / 2,
(display.height() - LOGO_HEIGHT) / 2,
armed, LOGO_WIDTH, LOGO_HEIGHT, 1);
delay(1);
}
if ( digitalRead(Safety) == HIGH)
{
display.clearDisplay();
display.drawBitmap(
(display.width() - LOGO_WIDTH ) / 2,
(display.height() - LOGO_HEIGHT) / 2,
disarmed, LOGO_WIDTH, LOGO_HEIGHT, 1);
delay(1);
}
else
{
display.clearDisplay();
}
display.display();
}