(Apologies if this is the wrong place for this)
I am attempting to make a project that sends a wireless signal if the light level in a room is below above a certain threshold. I am using the rc-switch library to send the wireless signal and the Adafruit_SSD1306 library to drive an oled (over i2c) which is used to configure stuff. I also have a DS3231 for keeping time.
I wrote most of the code for the OLED and all of the code for the real time clock and then decided to implement the wireless aspect of the project. However, whenever the code contains the send method for the wireless transmitter, even if it has not yet ran, the arduino begins to act erratic.
The duel nested loop in the drawRect() function does not loop correctly (called from drawHeader). The inside loop will run fine once and then the outside loop will stop at 1 (or sometimes it will go up to 7 and the inside loop will go forever). Also the LED i have on digital pin 9 will go high. The program doesn't proceed any further and thus never makes it out of setup.
Does anyone by any chance know of a solution or an alternate library for the wireless? Wireless is being used to emulate an RF remote. Does anyone know whats causing the pins to randomly go high and the variables to change?
(code in comments)
Please note I did have to remove some stuff to get around post size limit.
#include <Wire.h>
#include <DS3231.h>
#include <Adafruit_SSD1306.h>
#include <RCSwitch.h>
//Hardware stuff
#define button1 5
#define button2 6
#define button3 7
#define button4 8
#define led1 9
#define led2 11
#define ldr A0
#define antPin 10
//Display Initialization
Adafruit_SSD1306 display(128, 64, &Wire, -1);
//RTC Initialization
DS3231 Clock;
RTClib RTC;
DateTime getTime; //Make a dateTime object called getTime
int buttonState[] = {0,0,0,0};
int darkTrigger = 50;
int lightTrigger = 350;
boolean brightEnough = false;
boolean outletOn = false;
void setup()
{
Serial.begin(115200); //Begin Serial communications
Serial.println("Serial OK!"); //Test that serial works
Wire.begin(); //Begin I2C communications
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Initialize the display on address 0x3C
display.clearDisplay(); //Clear the display to stop logos from showing
display.display(); //Show the blank display
getTime = RTC.now(); //Set getTime to the current time
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(ldr, INPUT);
if(getTime.year() == 2000) //If the RTC reports a year of 2000, time was lost
{
resetTime();//Code never gets past this point
}
Clock.setYear(2001);
}
void loop()
{
display.clearDisplay();
readLDR();
updateOutlet();
display.display();
}
void resetTime()
{
display.clearDisplay();
drawHeader("SET TIME"); //This line is where the code stops working
if(getTime.year() == 2000) //Is the user reseting time or just changing it?
{
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("Time was lost");
}
else
{
display.println("");
}
display.println("");
display.setTextSize(2);
boolean doneSetting = false;
int sethour = 0;
int setminute = 0;
while(doneSetting == false)
{
display.setCursor(0,33);
drawBlackRect(0,33,128,64);
display.setTextSize(2);
display.print(form(sethour));
display.print(":");
display.println(form(setminute));
readButtons();
if(buttonState[0] == 1) //If user pressing s1
{
sethour++;
if(sethour > 23)
{
sethour = 0;
}
while(buttonState[0] == 1)
{
readButtons();
//Do nothing until button is released
}
}
if(buttonState[2] == 1) //If user pressing s3
{
setminute++;
if(setminute > 59)
{
setminute = 0;
}
while(buttonState[2] == 1)
{
readButtons();
//Do nothing until button is released
}
}
if(buttonState[3] == 1) //If user pressing s4
{
display.setCursor(0,54);
display.setTextSize(1);
display.print("Confirm?");
display.display();
while(buttonState[3] == 1)
{
readButtons();
delay(100);
//Do nothing until button is released
}
Serial.println("Released");
while((buttonState[0] == 0) && (buttonState[2] == 0) && (buttonState[3] == 0))
{
readButtons();
if(buttonState[3] == 1)
{
doneSetting = true;
Clock.setHour(sethour);
Clock.setMinute(setminute);
Clock.setSecond(0);
}
}
Serial.println("Exiting while");
}
display.display();
}
}
void drawHeader(String headerText) //Helper function that draws headers and makes their block font more readable
{
display.setCursor(1,1);
display.setTextSize(2);
display.setTextColor(BLACK); //Header must constrast background
drawRect(0,0,128,16);//Code never gets past this point
int numSpaces = 10 - headerText.length(); //How many spaces are needed to center the header text?
if(numSpaces < 0)
{
numSpaces = 0;
}
for(int i = 0; i < (numSpaces/2); i++) //Spaces before text to center
{
display.print(" ");
}
display.print(headerText);
for(int i = 0; i < (numSpaces/2); i++) //Spaces after text to center
{
display.print(" ");
}
display.println("");
display.setTextColor(WHITE);
}
void drawRect(int a, int b, int c, int d)
{
for(int i = a; i < c; i++)
{
for(int q = b; q < d; q++)
{
Serial.print("(");
Serial.print(i);
Serial.print(",");
Serial.print(q);
Serial.println(")");
delay(600);
display.drawPixel(i, q, WHITE);
}
Serial.println("Done with inner loop");
Serial.print("i (incrementing) is: ");
Serial.print(i);
Serial.print(" and ends at C:");
Serial.println(c);
}
}
void drawBlackRect(int a, int b, int c, int d)
{
for(int i = a; i < c; i++)
{
for(int q = b; q < d; q++)
{
display.drawPixel(i, q, BLACK);//Code acts up here
}
}
}
void readButtons()
{
buttonState[0] = 0;
buttonState[1] = 0;
buttonState[2] = 0;
buttonState[3] = 0;
if(digitalRead(button1) == LOW)
{
if(digitalRead(button1) == LOW)
{
buttonState[0] = 1;
}
}
if(digitalRead(button2) == LOW)
{
if(digitalRead(button2) == LOW)
{
buttonState[1] = 1;
}
}
if(digitalRead(button3) == LOW)
{
if(digitalRead(button3) == LOW)
{
buttonState[2] = 1;
}
}
if(digitalRead(button4) == LOW)
{
if(digitalRead(button4) == LOW)
{
buttonState[3] = 1;
}
}
}
void readLDR()
{
int ldrvalue = analogRead(ldr);
if(ldrvalue > lightTrigger)
{
brightEnough = true;
}
else if(ldrvalue < darkTrigger)
{
brightEnough = false;
}
}
void updateOutlet()
{
if(brightEnough)
{
turnOnOutlet();
}
else
{
turnOffOutlet();
}
}
void turnOnOutlet()
{
Serial.println("Trying to outlet on");
if(outletOn == false)
{
//wireless Initialization
RCSwitch wireless = RCSwitch();
wireless.enableTransmit(antPin); //Enable wireless transmitter
wireless.setPulseLength(308);
wireless.send("111011001010011000001111");
delay(3000);
outletOn = true;
}
}
void turnOffOutlet()
{
if(outletOn == true)
{
//wireless.send("111011001010011000001110");
delay(100);
outletOn = false;
}
}
Figured out a solution, not the problem. Removed all Serial prints, made prints to OLED display.print(F("text")). Still feel like its on the edge of imploding but it works. My guess is the arduino is running out of RAM and the IDE is just bad at guessing the usage.