I have used freeRam and it says that I have 952 bytes left, but when I increase my two byte arrays, the program will not run. I put a Serial.println(freeMemory()); at the beginning of setup and I am getting a repeating 6 in the serial monitor.
Note: Usually my program communicates to the computer via a C++ application, however for debugging I use the Serial monitor.
Here's the code:
#include <MemoryFree.h>
/*50 X 8 Scrolling Matrix
Author: Kurt
Date 2/27/14
Version 3.0 */
#include "DisplayArduinoClass.h"
DisplayArduinoClass obj;
bool first;
void setup(){
Serial.print(freeMemory());
DisplayArduinoClass obj;
Serial.begin(115200, SERIAL_8N2); // Begin serial at 256000 baud and two stop bits
obj.Setup(); // Setup pins and set as outputs
obj.clearByteMaps(); // Clear message byte maps
obj.Shift = 0; // Set Shift Index to zero
obj.refresh(); // Display message byte maps, which is nothing
obj.confirmSerialConnection(); // Confirm the serial connection
first = 1;
}
void loop(){
if(first==1){ // Code to run only once in loop (Can't be in setup)
obj.Setup(); // Setup pins and set as outputs
first=0; // Make code not run again till micro-controller is reset
obj.recieveSpeed(); // Recieve the scrolling speed
obj.numberOfCols = 0; // Set number of columns to five
Serial.write(114); // Send Ready Status
}
obj.numberOfCols = 0; // Set number of columns to zero
obj.recieveNumberOfCols(); // Recieve number of columns
obj.clearByteMaps(); // Clear message byte maps
Serial.write(114); // Send Ready Status
obj.recieveRmsgByteMap(); // Recieve red message byte map
Serial.write(114); // Send Ready Status
obj.recieveGmsgByteMap(); // Recieve green message byte map
Serial.print(freeMemory());
obj.scrollByteMaps(); // Scroll message byte maps
}
#include "DisplayArduinoClass.h"
#include "Arduino.h"
#include <stdlib.h>
void DisplayArduinoClass::refresh(){
byte rows;
for(byte row=0; row<8; row++){ // Switch rows so that it isn't upside down
switch (row){
case 0: rows = 7; break;
case 1: rows = 6; break;
case 2: rows = 5; break;
case 3: rows = 4; break;
case 4: rows = 3; break;
case 5: rows = 2; break;
case 6: rows = 1; break;
case 7: rows = 0; break;
}
digitalWrite (rowE, HIGH); //Turn OFF display
digitalWrite (rclk, LOW); //Turn OFF display
for(byte col=0;col < 50; col++){ // For each column
digitalWrite(gdat, LOW); // Make sure dataPins are LOW
digitalWrite(rdat, LOW); // Make sure dataPins are LOW
if (bitRead (rmsgByteMap[col+Shift], rows)){ // If bit is present in red message byte map
digitalWrite(rdat, HIGH); // Then write red serial data high
}
else digitalWrite (rdat, LOW); // If not, Then write red serial data low
if (bitRead (gmsgByteMap[col+Shift], rows)){ // If bit is present in green message byte map
digitalWrite(gdat, HIGH); // Then write green serial data high
}
else digitalWrite (gdat, LOW); // If not, Then write green serial data low
digitalWrite(clk, HIGH); // Increment clock
digitalWrite(clk, LOW); // Increment clock
}
digitalWrite(rclk, HIGH); // Increment row clock
if bitRead(row,0) digitalWrite (row1, HIGH); else digitalWrite(row1, LOW); // Select row to turn on for multiplexing
if bitRead(row,1) digitalWrite (row2, HIGH); else digitalWrite(row2, LOW); // Select row to turn on for multiplexing
if bitRead(row,2) digitalWrite (row3, HIGH); else digitalWrite(row3, LOW); // Select row to turn on for multiplexing
digitalWrite(rowE, LOW); // Turn display on
delayMicroseconds(500); // Pause for Multiplexing
}
}
void DisplayArduinoClass::Setup(){
row1 = 5; // Set micro-controller pins
row2 = 10; // Set micro-controller pins
row3 = 6; // Set micro-controller pins
rowE = 11; // Set micro-controller pins
rclk = 9; // Set micro-controller pins
clk = 4; // Set micro-controller pins
rdat = 2; // Set micro-controller pins
gdat = 3; // Set micro-controller pins
pinMode (row1, OUTPUT); // Set pins as OUTPUTS
pinMode (row2, OUTPUT); // Set pins as OUTPUTS
pinMode (row3, OUTPUT); // Set pins as OUTPUTS
pinMode (rowE, OUTPUT); // Set pins as OUTPUTS
pinMode (rclk, OUTPUT); // Set pins as OUTPUTS
pinMode (clk , OUTPUT); // Set pins as OUTPUTS
pinMode (rdat, OUTPUT); // Set pins as OUTPUTS
pinMode (gdat, OUTPUT); // Set pins as OUTPUTS
Shift = 0;
}
void DisplayArduinoClass::clearByteMaps(){
for(int i=0;i<429;i++){rmsgByteMap[i] = 0;} // Clear rmsgByteMap
for(int i=0;i<429;i++){gmsgByteMap[i] = 0;} // Clear gmsgByteMap
}
void DisplayArduinoClass::confirmSerialConnection(){
char incomingString[6] = {0,0,0,0,0,0}; // Declare array for incoming string
while(!Serial.available()); // Wait for input
Serial.readBytesUntil(101,incomingString, 6); // Read until termination character, read into..., read maximum of 5 bytes
if (incomingString [0] == 'H' && incomingString [1] == 'o' && incomingString [2] == 'l' && incomingString [3] == 'a' && incomingString [4] == '?'){ //If recieved "Hola?"
Serial.write("Hola!"); // Then send back "Hola!"
}
}
void DisplayArduinoClass::recieveSpeed(){
while(!Serial.available()); // Wait for input
Serial.readBytesUntil(101,(char*)Speed, 2); // Read until termination character, read into..., read maximum of 2 bytes
}
void DisplayArduinoClass::recieveNumberOfCols(){
byte val [3] ={0,0,0}; // Stores two incoming bytes
while(!Serial.available()); // Wait for input
Serial.readBytesUntil(101,(char*)val, 3); // Read until termination character, read into..., read maximum of 3 bytes
numberOfCols = val[0] * 256 + val[1]; // Put together two bytes to form int and numberOfCols in the message
}
void DisplayArduinoClass::recieveRmsgByteMap(){
while(!Serial.available()); // Wait for input
Serial.readBytesUntil(101,(char*)&rmsgByteMap[50], numberOfCols+1); // Read until termination character, read into..., read maximum of numberOfCols bytes
}
void DisplayArduinoClass::recieveGmsgByteMap(){
while(!Serial.available()); // Wait for input
Serial.readBytesUntil(101,(char*)&gmsgByteMap[50], numberOfCols+1); // Read until termination character, read into..., read maximum of numberOfCols bytes
}
void DisplayArduinoClass::scrollByteMaps(){
bool First = 1;
do{
Shift = 0; // Set shift index to zero
for(int j=0;j<numberOfCols+51;j++){ // For length of message
for(byte i=0;i<Speed[0];i++){ // For speed
refresh(); // display message byte maps
}
Shift++; // Increment shift
if (First==1){
Serial.write(114); // Send Ready Status
First=0;
}
}
} while(!Serial.available()); // Do until user enters a new message
}