Go Down

Topic: Uno - Use multiple SPI slaves: TFT and SD card (Read 850 times) previous topic - next topic

27sharp

Hello,

I Want to use two SPI devices on Arduino uno. Both uses the same pins.
First I try it with different cs pins. The TFT Display use Pin 10 and the SD card Pin 4.

While starting the Sketch Im able to access to the sd card, but i want to update the value and read again from the sd card, but it doesn't work.

my code:
Code: [Select]
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9340.h"
#include "Sodaq_DS3231.h"
#include <SD.h>
#define MAX_ZEILE 210
//#include <Fonts/FreeSansBold18pt7b.h>

// These are the pins used for the UNO
// for Due/Mega/Leonardo use the hardware SPI pins (which are different)
#define _sclk 13
#define _miso 12
#define _mosi 11
#define _cs 10
#define _dc 9
#define _rst 8



Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);


String dateString;
int minuteNow=0;
int minutePrevious=0;
int dateNow=0;
int datePrevious=0;
char timeChar[80];
char dateChar[50] = {"00.00.0000"};
int zeile;
DateTime now;


const int READ_BUFFER_SIZE = 40;
char serialBuffer[READ_BUFFER_SIZE];
File myFile;

char *str [7] = {"0"};
char datum [50] = {"15.06.2016"};

void setup() {
  pinMode(4,OUTPUT);    // SD Card.
  digitalWrite(4,HIGH); 

  pinMode(10,OUTPUT);    // TFT
  digitalWrite(10,HIGH);
 
 
 
  SPI.begin();
  SD.begin(4);
  setRTCTime();
  tft.begin();
  rtc.begin();
 
 
  myFile = SD.open("test.txt");
  getRow(dateChar);
   
 
 
  tft.fillScreen(ILI9340_BLACK);
  tft.setRotation(3);
   
 
 
 
 
      printText(str[0],ILI9340_YELLOW,43,100,2);
      printText(str[1],ILI9340_YELLOW,43,120,2);
      printText(str[2],ILI9340_YELLOW,43,140,2);
      printText(str[3],ILI9340_YELLOW,43,160,2);
      printText(str[4],ILI9340_YELLOW,43,180,2);
     
      getDate();
 
   printText(dateChar, ILI9340_YELLOW,43,56,4);
 
}
uint32_t old_ts;
void loop() {
   now = rtc.now(); //get the current date-time

   uint32_t ts = now.getEpoch();

    if (old_ts == 0 || old_ts != ts) {
      old_ts = ts;
 
    minuteNow = now.minute();
    if(minuteNow!=minutePrevious) {
      minutePrevious = minuteNow;
      getTime();
      tft.fillRect(64, 0, 200, 55, ILI9340_BLACK);
      printText(timeChar, ILI9340_WHITE,65,5,6);
     
    }
    dateNow = now.date();
    if(dateNow!=datePrevious) {
      datePrevious = dateNow;
      getDate();
     getRow(dateChar);
   
      printText(str[0],ILI9340_YELLOW,220,100,2);
     
     
     
     
     
      tft.fillRect(44,57,240,30,ILI9340_BLACK);
      printText(dateChar, ILI9340_YELLOW,43,56,4);
     
     
     }
     
   }
 
  delay(1000);

}

bool readStream(File& myFile)
{
  static byte index;
 
  while (myFile.available())
  {
    char c = myFile.read();

    if (c >= 32 && index < READ_BUFFER_SIZE - 1)
    {
      serialBuffer[index++] = c;
     
    }
    else if (c == '\n' && index > 0)
    {
      serialBuffer[index] = '\0';
      index = 0;
      return true;
    }
  }

  return false;
}



void getRow(char *datum){
  int i = 0;
  int z = 0;
 
 
  digitalWrite(10,HIGH);
   digitalWrite(4,LOW); 
  while(i<MAX_ZEILE && readStream(myFile)){
     
     if(strcmp(datum,serialBuffer) == 0){
     zeile = (i-1);
     
     
     }
   
     if(i>=zeile&& i<=(zeile+7) ){
      str[z] = strdup(serialBuffer);
      z++;
     
    }
     
     i++;
  }
    digitalWrite(4,HIGH); 
     digitalWrite(10,LOW);
}


Thanks in advance.

Go Up