SD module and led strip

Hi,

I'm trying to build a timer that shows on an LCD display that records for how long the arduino has been running. I would like to save as well the previous running time. For this, I'm using an SD module from Velleman (VMA202) that works with the standard SD library. I have a button as well to reset the timer. I'm using an arduino uno and products from seeed studio for convenience (Base shield, TTL buttons, ...).

Now, I came accross some led strips, as well from seeed, and decided it could be fun to try them on, having them being turned on by a second button. They run nicely but the SD doesn't work anymore, even though it still detects it and sees the file. As far as I know, these are not on the same pins: the SD is on pin 10, whereas the others are on pins 1 to 7.

Any help would be appreciated.

My code is the following:

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include "TM1637.h"
#include <EEPROM.h>
#include <TimerOne.h>
#include <SPI.h>
#include <SD.h>

#include "Adafruit_NeoPixel.h"
#ifdef __AVR__
#include <avr/power.h>
#endif

// Definitions for screen
#define I2C_ADDR    0x27
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
// Defintions for timer
#define ON 1
#define OFF 0
int8_t TimeDisp[] = {0x00, 0x00, 0x00, 0x00};
unsigned char ClockPoint = 1;
unsigned char Update;
unsigned char halfsecond = 0;
unsigned char second;
unsigned char minute = 0;
unsigned char hour = 12;
String convert;
// Misc definitions
File myFile;
int n = 1;
int addr = 0;
int buttonState = 0;
int buttonState2 = 0;
int ledon = 0;
// Pins definitions for TM1637 and can be changed to other ports
const int CLK = 3;
const int DIO = 4;
// Pins defintions for buttons
const int buttonPin = 2;
const int buttonPin2 = 5;
// Pins definitions for led strips
#define PIN 6
#define PINI 7
#define NUMPIXELS 60

// Call screens and led strips
// Commenting the next 2 lines allows the SD to work.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels2 = Adafruit_NeoPixel(NUMPIXELS, PINI, NEO_GRB + NEO_KHZ800);
TM1637 tm1637(CLK, DIO);
LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

void setup()
{ // Open serial communication
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // Start 16x2 LCD
  lcd.begin (16, 2);
  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();
  lcd.print("Init. SD");
  Serial.print("Initializing SD card...");
  // Port for the SD card: 10
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    lcd.print("SD failed");
    while (1);
  }
  lcd.home ();
  lcd.print("Init. done.");
  Serial.print("\nInitialization done.\n");

  // Open file stored on SD to take out last recorded time
  lcd.clear();
  lcd.home ();
  // Print error if file not found
  if (SD.exists("lasttime.txt") == false) {
    Serial.print("No file\n");
  }
  myFile = SD.open("lasttime.txt", FILE_READ);
  // Print if no access to file
  if (myFile.available() == false) {
    Serial.print("Error\n");
  }
  while (myFile.available()) {
    char letter = myFile.read();
    lcd.print(letter);
    Serial.print(letter);
  }
  myFile.close();
  lcd.print(" - previous");

  // Initialization Led strips
// Comment the next 7 lines for the SD to work.
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  pixels.setBrightness(100);
  pixels2.setBrightness(100);
  pixels.begin();
  pixels2.begin();

  // Start 4 digits display
  tm1637.init();
  tm1637.set(7);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
  // Start timer
  Timer1.initialize(500000);//timing for 500ms
  Timer1.attachInterrupt(TimingISR);//declare the interrupt serve routine:TimingISR
}


void loop()
{
  // Define strings to write in SD file
  String dataString = "";
  String convert1 = "";

  // Reset counter when pressing on button
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    n = 0;
    second = 0;
    minute = 0;
    hour = 0;
    lcd.clear();
    tm1637.init();
    // Write
    myFile = SD.open("lasttime.txt", FILE_READ);
    while (myFile.available()) {
      //Serial.write(myFile.read());
      char letter = myFile.read();
      lcd.print(letter);
      convert1 += String(letter);
    }
    lcd.print(" - previous");
    myFile.close();
    Serial.print(convert1);
    Serial.print("\n");
    delay(500);

  }

// Comment the next block for the SD to work
  buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 == HIGH) {
  // If led off, turn it on
    if (ledon == 0){
    for(int i=0;i<NUMPIXELS;i++){
      pixels.setPixelColor(i, pixels.Color(0,102,204));
      pixels2.setPixelColor(i, pixels2.Color(0,102,204));
      pixels.show();
      pixels2.show();
      ledon = 1;}}
  // If led on, turn it off
   else {for(int i=0;i<NUMPIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(0,0,0));
    pixels2.setPixelColor(i, pixels2.Color(0,0,0));
    pixels.show();
    pixels2.show();
    ledon = 0;}}
    }

  // Update timer
  if (Update == ON) {
    TimeUpdate();
    tm1637.display(TimeDisp);
    lcd.setCursor (0, 1);       // go to start of 2nd line
    lcd.print(minute / 10, DEC);
    lcd.setCursor (1, 1);
    lcd.print(minute % 10, DEC);
    lcd.setCursor (2, 1);       // go to start of 2nd line
    lcd.print(":");
    lcd.setCursor (3, 1);
    lcd.print(second / 10, DEC);
    lcd.setCursor (4, 1);
    lcd.print(second % 10, DEC);
    lcd.print(" - current");
    String dataString = "";
    dataString = String(minute / 10) + String(minute % 10) + ":" + String(second / 10) + String(second % 10);
    myFile = SD.open("lasttime.txt", O_RDWR);
    //if (myFile) {Serial.print("file available");}
    myFile.print(dataString);
    myFile.close();
  }
}

// Timing function
void TimingISR() {
  halfsecond ++;
  Update = ON;
  if (halfsecond == 2) {
    second ++;
    if (second == 60) {
      minute ++;
      if (minute == 60) {
        hour ++;
        if (hour == 24) {
          hour = 0;
        }
        minute = 0;
      }
      second = 0;
    }
    halfsecond = 0;
  }
  // Serial.println(second);
  ClockPoint = (~ClockPoint) & 0x01;
}

// Update 4-digits display function
void TimeUpdate(void) {
  if (ClockPoint) {
    tm1637.point(POINT_ON);
  } else {
    tm1637.point(POINT_OFF);
  }
  TimeDisp[0] = minute / 10;
  TimeDisp[1] = minute % 10;
  TimeDisp[2] = second / 10;
  TimeDisp[3] = second % 10;
  Update = OFF;
}

Oops

I see, I wasn't aware of that. That being said, the LCD screen still works while the serial runs. I've tried too to remove all serial commands and nothing changed...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.