DANG SPI!!!!!!

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
rob drizzle
 
Posts: 127
Joined: Wed May 04, 2011 6:14 pm

DANG SPI!!!!!!

Post by rob drizzle »

I'm working on a project that has a nRF2.4 radio and uses an SD card (adafruit datalogger shield). I know the card is good. I know that the code works without the SD perfectly. When I add the code for the SD card... The code just goes into a loop at "Initializing SD card... card initialized." Writing it over and over. It writes blank files to the card with no header ("millis,stamp,datetime,light,temp,vcc"), completely blank. How do I fix the dang SPI conflict!? (assuming thats what it is).

Code (Partial to void loop, which it never reaches):

Code: Select all

/*
Base only code...
 */

#include <serialGLCDlib.h>
#include <avr/pgmspace.h>
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include "DHT.h"
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>

#define DHTPIN 2 // DHT Pin
#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE);
RTC_DS1307 RTC;


#define LOG_INTERVAL 1000
#define SYNC_INTERVAL 1000
uint32_t syncTime = 0;

#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()

const int chipSelect = 10;

// the logging file
File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  
  // red LED indicates error
//  digitalWrite(redLEDpin, HIGH);

  while(1);
}

// Avoid spurious warnings
#undef PROGMEM 
#define PROGMEM __attribute__(( section(".progmem.data") )) 
#undef PSTR 
#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))

RF24 radio(8,9); //Radio pins 3(to8),4(to9)
RF24Network network(radio);

// Our node address
uint16_t this_node;

// The message that we send is just an unsigned int, containing a sensor reading.
struct message_t
{
int temp_reading;
int humid_reading;
int voltage_reading;
int reset_reading;
  message_t(void): temp_reading(0), humid_reading(0), voltage_reading(0), reset_reading(0) {}//ADD VOLTAGE READING!!!!
};

int lcdCount = 0;
int tempf = 0;
int humidrh = 0;
int voltage = 0;
int reset = 0;

char nodereset[10] = "RS";
char nodetemp1[10] = "ND";
char humid1[10] = "ND";
char voltage1[10] = "ND";
char nodetemp2[10] = "ND";
char humid2[10] = "ND";
char voltage2[10] = "ND";
char nodetemp3[10] = "ND";
char humid3[10] = "ND";
char voltage3[10] = "ND";
int nodetemp4 = 0;
int humid4 = 0;
char voltage4[10] = "ND";
char batstat1[14] = "Odr Bat Low!";
char batstat2[14] = "Idr Bat Low!";
char batstat3[14] = "Atc Bat Low!";
char sysStat[13] = "Sys Stus:";
char sysStatc[14] = "All Ok       ";

//-------------------------------------------------------------------------------

void setup(void)
{

  
//
// Print preamble
//
  
  Serial.begin(115200);
  delay(5000);
  
//
// Pull node address out of eeprom 
//

  // Which node are we?
  this_node = 0;
    
  //
  //DHT sensor
  //  

  dht.begin();
  Wire.begin();
  RTC.begin();
  
    // initialize the SD card
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
  Serial.println("card initialized.");
  
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break; // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
  

  logfile.println("millis,stamp,datetime,light,temp,vcc");
  
  //
  // Bring up the RF network
  //

  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 92, /*node address*/ this_node);
}

serialGLCD lcd; // initialisation


//------------------------------------------------------------------------------------

void loop(void)
{
//  lcd.reverseColor();
if ( lcdCount == 0 ){
  lcd.clearLCD();
  
    DateTime now = RTC.now();
    

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: DANG SPI!!!!!!

Post by pburgess »

Possibly running out of RAM...that's a whole lot of libraries and whatnot. If you have access to a Mega, I'd suggest trying it there and see if it exhibits the same symptoms. Alternately, maybe try minimizing the number and length of global strings, and stick as many in PROGMEM as possible.

User avatar
rob drizzle
 
Posts: 127
Joined: Wed May 04, 2011 6:14 pm

Re: DANG SPI!!!!!!

Post by rob drizzle »

Maybe your right... I have changed some things around and located what I could to PROGMEM.

The sketch is 27090 bytes as it appears here.

Code: Select all

/*
Base only code...
 */

#include <serialGLCDlib.h>
#include <avr/pgmspace.h>
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
//#include "DHT.h"
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>

//#define DHTPIN 2 // DHT Pin
//#define DHTTYPE DHT22   // DHT 22
//DHT dht(DHTPIN, DHTTYPE);
RTC_DS1307 RTC;

#define P(name) const prog_uchar name[] PROGMEM // declare a PROGMEM string
#define LOG_INTERVAL 1000
#define SYNC_INTERVAL 1000
uint32_t syncTime = 0;


const int chipSelect = 10;

// the logging file
File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  
  // red LED indicates error
//  digitalWrite(redLEDpin, HIGH);

  while(1);
}

// Avoid spurious warnings
#undef PROGMEM 
#define PROGMEM __attribute__(( section(".progmem.data") )) 
#undef PSTR 
#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))

RF24 radio(8,9); //Radio pins 3(to8),4(to9)
RF24Network network(radio);

// Our node address
const int this_node = 0;

// The message that we send is just an unsigned int, containing a sensor reading.
struct message_t
{
int temp_reading;
int humid_reading;
int voltage_reading;
int reset_reading;
  message_t(void): temp_reading(0), humid_reading(0), voltage_reading(0), reset_reading(0) {}//ADD VOLTAGE READING!!!!
};

bool lcdCount = 0;
int tempf = 0;
int humidrh = 0;
int voltage = 0;
int reset = 0;

char nodereset[3] = "RS";
char nodetemp1[4] = "ND";
char humid1[4] = "ND";
char voltage1[4] = "ND";
char nodetemp2[4] = "ND";
char humid2[4] = "ND";
char voltage2[4] = "ND";
char nodetemp3[4] = "ND";
char humid3[4] = "ND";
char voltage3[4] = "ND";
int nodetemp4 = 0;
int humid4 = 0;
P(batstat1) = "Odr Bat Low!";
P(batstat2) = "Idr Bat Low!";
P(batstat3) = "Atc Bat Low!";
P(sysStat) = "Sys Stus:";
P(sysStatc) = "All Ok       ";

//-------------------------------------------------------------------------------

void setup(void)
{

  
//
// Print preamble
//
  
  Serial.begin(115200);
  delay(5000);
  
//
// Pull node address out of eeprom 
//
    
  //
  //DHT sensor
  //  

//  dht.begin();
  Wire.begin();
  RTC.begin();
  
    // initialize the SD card
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
  Serial.println("card initialized.");
  
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break; // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
  

  logfile.println("millis,stamp,datetime,light,temp,vcc");
  
  //
  // Bring up the RF network
  //

  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 92, /*node address*/ this_node);
}

serialGLCD lcd; // initialisation


//------------------------------------------------------------------------------------

void loop(void)
{
//  lcd.reverseColor();
if ( lcdCount == 0 ){
  lcd.clearLCD();
  
    DateTime now;
    
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
  
// log milliseconds since starting
//uint32_t m = millis();
//logfile.print(m); // milliseconds since start
//logfile.print(", ");

//----------------------- time BANNED -------------------------------------------

  // fetch the time
  now = RTC.now();
  // log time
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print("/");
  logfile.print(now.year(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print('"');
    
lcd.gotoLine(7);
Serial.print("                     ");
delay(10);
lcd.gotoLine(7);
Serial.print(now.month(), DEC);
Serial.print("/");
delay(10);
Serial.print(now.day(), DEC);
Serial.print("/");
delay(10);
Serial.print(now.year(), DEC);
Serial.print(" ");
delay(10);
Serial.print(now.hour(), DEC);
Serial.print(":");
delay(10);
Serial.print(now.minute(), DEC);
Serial.print(" Now");
    
lcd.gotoLine(8);
Serial.print("                     ");
delay(10);
lcd.gotoLine(8);
Serial.print(now.month(), DEC);
Serial.print("/");
delay(10);
Serial.print(now.day(), DEC);
Serial.print("/");
delay(10);
Serial.print(now.year(), DEC);
Serial.print(" ");
delay(10);
Serial.print(now.hour(), DEC);
Serial.print(":");
delay(10);
Serial.print(now.minute(), DEC);
Serial.print(" Start");
delay(10);
   
// Print title information 
lcd.gotoLine(1);
Serial.print("Local  Temp  RH%  Bat");

lcd.gotoLine(4);
Serial.print("     BOOTING  UP  ");

lcdCount = 1;
}
  
//humid4 = dht.readHumidity();
//int t = dht.readTemperature();
nodetemp4 = (1.8 + 32); //ADD t BACK IN!!!!! 

// Pump the network regularly
  network.update();

  // If we are the base, is there anything ready for us?
  while ( network.available() )
  {
    // If so, grab it and print it out
    RF24NetworkHeader header;
    message_t message;
    network.read(header,&message,sizeof(message));

//------------------- Converting and Storing radio data ---------------------------------

tempf = (message.temp_reading * 1.8 + 32);
humidrh = (message.humid_reading);
voltage = (message.voltage_reading);
reset = (message.reset_reading);

//--------------------Storing Node Number for sorting use -----------------------------------

int nodedump = header.from_node;

//------------------------- Sorting received information from radio ---------------------------

switch(nodedump) // If node dump reads "1" through blah is node. Need to assign Attic, Outdoor, etc. 
{
case 1: // no "0" because we will hard code it in.
itoa(tempf, nodetemp1,4); //converting tempf to nodetemp1 for display and storage
itoa(humidrh, humid1, 4); //converting humidrh to humid1 for display and storage
itoa(voltage, voltage1, 4);
break;
case 2:
humidrh = humidrh - 2; //calibration indoor sensor humid is +2
itoa(tempf, nodetemp2, 4);
itoa(humidrh, humid2, 4);
itoa(voltage, voltage2, 4);
break;
case 3:
humidrh = humidrh - 5; //calibration attic sensor humid is +5
itoa(tempf, nodetemp3, 4);
itoa(humidrh, humid3, 4);
itoa(voltage, voltage3, 4);
break;
default :
break;
}

//------------------------- Battery Warning -------------------------------

if (voltage < 25){
switch(nodedump) 
{
case 1:
if (voltage < 25){
lcd.gotoLine(6);
Serial.print("                     ");
delay(10);
lcd.gotoLine(6);
printP(sysStat);
delay(10);
printP(batstat1);
delay(10);
}
break;
case 2:
if (voltage < 25){
lcd.gotoLine(6);
Serial.print("                     ");
delay(10);
lcd.gotoLine(6);
printP(sysStat);
delay(10);
printP(batstat2);
delay(10);
}
break;
case 3:
if (voltage < 25){
lcd.gotoLine(6);
Serial.print("                     ");
delay(10);
lcd.gotoLine(6);
printP(sysStat);
delay(10);
printP(batstat3);
delay(10);
}
break;
default :
break;
}
}
else
{
lcd.gotoLine(6);
Serial.print("                     ");
delay(10);
lcd.gotoLine(6);
printP(sysStat);
delay(10);
printP(sysStatc);
delay(10);
}
//------------------------- Reset -------------------------------

switch(nodedump)
{
case 1:
if(reset == 1){
strcpy(nodetemp1, nodereset);
strcpy(humid1, nodereset);
//sdWrite();
}
break;
case 2:
if(reset == 1){
strcpy(nodetemp2, nodereset);
strcpy(humid2, nodereset);
//sdWrite();
}
break;
case 3:
if(reset == 1){
strcpy(nodetemp3, nodereset);
strcpy(humid3, nodereset);
//sdWrite();
}
break;
default :
break;
}

// setting cursor for printing on second line of display...
lcd.gotoLine(2);
Serial.print("                     ");
delay(10);
lcd.gotoLine(2);
Serial.print("Base ");
delay(10);
lcd.gotoPosition(43,8);
Serial.print(nodetemp4);
Serial.print("F ");
delay(10);
lcd.gotoPosition(79,8);
Serial.print(humid4);
Serial.print("%");
delay(10);
lcd.gotoPosition(109,8);
Serial.print("A/C");
delay(10);

lcd.gotoLine(3);
Serial.print("                     ");
delay(10);
lcd.gotoLine(3);
Serial.print("Indr ");
delay(10);
lcd.gotoPosition(43,16);
Serial.print(nodetemp2  );
Serial.print("F ");
delay(10);
lcd.gotoPosition(79,16);
Serial.print(humid2);
Serial.print("%");
delay(10);
lcd.gotoPosition(109,16);
Serial.print(voltage2);
delay(10);

lcd.gotoLine(4);
Serial.print("                     ");
delay(10);
lcd.gotoLine(4);
Serial.print("Attc ");
delay(10);
lcd.gotoPosition(43,24);
Serial.print(nodetemp3);
Serial.print("F ");
delay(10);
lcd.gotoPosition(79,24);
Serial.print(humid3);
Serial.print("%");
delay(10);
lcd.gotoPosition(109,24);
Serial.print(voltage3);
delay(10);

lcd.gotoLine(5);
Serial.print("                     ");
delay(10);
lcd.gotoLine(5);
Serial.print("Otdr ");
delay(10);
lcd.gotoPosition(43,32);
Serial.print(nodetemp1);
Serial.print("F ");
delay(10);
lcd.gotoPosition(79,32);
Serial.print(humid1);
Serial.print("%");
delay(10);
lcd.gotoPosition(109,32);
Serial.print(voltage1);
delay(10);

  logfile.print(", ");
  logfile.print(nodetemp2);
  logfile.print(", ");
  logfile.print(humid2);
  logfile.print(", ");
  logfile.print(nodetemp3);
  logfile.print(", ");
  logfile.print(humid3);
  logfile.print(", ");
  logfile.print(nodetemp1);
  logfile.print(", ");
  logfile.print(humid1);
  
  
  logfile.println();

  // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
  
  // blink LED to show we are syncing data to the card & updating FAT!
  logfile.flush();
  delay(10);
  
  }
}
//-------------------------------------------------------------------------------------

// function to print a PROGMEM string
void printP(const prog_uchar *str)
{
char c;
while((c = pgm_read_byte(str++)))
Serial.print(c,BYTE);
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”