Go Down

Topic: Second time i read dir arduino halts (Read 1 time) previous topic - next topic

AllanB

I got my SC Card module working without problems.
In my setup, I use a 4x20 LCD for a menu. In this menu when I select "SC Card", the following sub should run.
And it does first time without any trouble - it shows names of 6 files on my SD Card. But second time it shows names for first 3 files and then get stuck.

Code: [Select]
void sdCard() {
  lcd.clear();
  int i=0; int j=0;
  String files [1][3];
  File root; File entry;
  root = SD.open("/");
  do {
    entry =  root.openNextFile();
    lcd.setCursor((j*10)+1, i);
    String ename=entry.name();
    files[j][i]=ename;
    lcd.print(ename.substring(0,ename.indexOf(".")));
    i = i + 1;
    if ((i==4)&&(j==0)) {j=1; i=0;}
  } while ((entry)&&(i+j<5));
  i=0; j=0;
  while (mode==2) {
    lcd.setCursor(j*10, i);
    lcd.print((char)0);     
    if (Button(btnZDown)) {
      while (Button(btnZDown)) {}
      lcd.setCursor(j*10,i);
      lcd.print(" ");
      i = i + 1;
      if (i==4) {
        if (j==1) j=0; else j=1;
        i=0;
      }
      if (files[j][i]=="") {i=0; j=0;}
    }
    if (Button(btnReverse)) {
      while (Button(btnReverse)) {}
      mode=0;
    }
  }
}

void loop() {
  if (Button(btnZDown)) {
    while (Button(btnZDown)) {}
    move_menu_cursor(1);
  }
  if (Button(btnZUp)) {
    while (Button(btnZUp)) {}
    move_menu_cursor(2);
  }
  if (Button(btnReverse)) {
    while (Button(btnReverse)) {}
    mode=menu_cursor;
    if (mode==1) togglemode();
    if (mode==2) sdCard();
    if (mode==3) serialRepeater();
    lcd.clear();
    mainmenu();
  }
}

Grumpy_Mike

When posting code you need to post all of it, so we can spot errors, not just bits of it.
If it is too long then cut it down so that it shows just the errors but post something that compiles and runs and shows the error.

AllanB

OK Here's the complete sketch:
Trouble's near the bottom.

Code: [Select]

/*
mode 0: Menu
mode 1: Toggle
mode 2: SD Card
mode 3: Serial input
*/

// include the library code:
#include <ShiftRegLCD.h>
#include <SoftwareSerial.h>
#include <SD.h>
#define rxPin 7
#define txPin 8
#define srdataPin 2
#define srclockPin 3

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;

const int chipSelect = 10; 

ShiftRegLCD lcd(srdataPin, srclockPin, TWO_WIRE, 4);
SoftwareSerial mySerial(rxPin, txPin);

float x = 0.000;
float y = 0.000;
float z = 0.000;
int latchPin  = 4;
int clockPin  = 5;
int dataPin  = 6;
int mode = 0;
int menu_cursor = 1;
byte arrow[8] = {16,24,28,30,28,24,16};
int serData;
byte switchVar1 = 72;
int btnYUp =1;
int btnXLeft = 2;
int btnXRight = 3;
int btnYDown = 4;
int btnForward = 5;
int btnReverse = 6;
int btnZDown = 7;
int btnZUp= 8;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.createChar(0, arrow);
  //lcd.begin(40, 4);
  // Print a message to the LCD.
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, INPUT);
// pinMode(rxPin, INPUT);
// pinMode(txPin, OUTPUT);
  pinMode(10, OUTPUT);
  SD.begin(chipSelect);
  mainmenu();
}

void mainmenu() {
  lcd.setCursor(0,0);
  lcd.print("MENU");
  lcd.setCursor(0,1);
  lcd.print(" Toggle mode");
  lcd.setCursor(0,2);
  lcd.print(" SD Card");
  lcd.setCursor(0,3);
  lcd.print(" Serial repeater");
  lcd.setCursor(0,menu_cursor);
  lcd.print((char)0);
}

// dir = 1 down dir=0 up
void move_menu_cursor(int dir) {
  if (dir==1) {
    if (menu_cursor<3) {
       menu_cursor ++;
       mainmenu();
    }
  }
  if (dir==2) {
    if (menu_cursor>1) {
       menu_cursor --;
       mainmenu();
    }
  }
}

void togglemode() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Togglemode");
  lcd.setCursor(0,1);
  lcd.print("X: 0.000");
  lcd.setCursor(0,2);
  lcd.print("Y: 0.000");
  lcd.setCursor(0,3);
  lcd.print("Z: 0.000");
  while (mode==1) {

    if (Button(btnZUp)) {
      while (Button(btnZUp)) {}
      z=z+0.100;
    }
    if (Button(btnZDown)) {
      while (Button(btnZDown)) {}
      z=z-0.100;
    }
    if (Button(btnYUp)) {
      while (Button(btnYUp)) {}
      y=y+0.100;
    }
    if (Button(btnYDown)) {
      while (Button(btnYDown)) {}
      y=y-0.100;
    }
    if (Button(btnXLeft)) {
      while (Button(btnXLeft)) {}
      x=x+0.100;
    }
    if (Button(btnXRight)) {
      while (Button(btnXRight)) {}
      x=x-0.100;
    }
    if (Button(btnReverse)) {
      while (Button(btnReverse)) {}
      mode=0;
    }
 
    lcd.setCursor(2,1);
    if (x>=0) {lcd.print(" ");}
    lcd.print(x);
    lcd.setCursor(2,2);
    if (y>=0) {lcd.print(" ");}
    lcd.print(y);
    lcd.setCursor(2,3);
    if (z>=0) {lcd.print(" ");}
    lcd.print(z);
  }
}

void serialRepeater(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Serial repeater");
  Serial.begin(9600);
  Serial.println("Ready ...");
  mySerial.begin(9600);
  mySerial.listen();
  while (mode==3) {   
    while (mySerial.available()) Serial.write(mySerial.read());
    while (Serial.available()) mySerial.write(Serial.read());
    if (Button(btnReverse)) {
      while (Button(btnReverse)) {}
      mode=0;
    }
  } 
}

byte shiftIn(int myDataPin, int myClockPin) {
  int i;
  byte myDataIn = 0;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
  for (i=0; i<8; i++)
  {
    digitalWrite(myClockPin, 0);
    delayMicroseconds(20);
    myDataIn = myDataIn << 1;
    myDataIn = myDataIn + digitalRead(myDataPin);
    digitalWrite(myClockPin, 1);
  }
  return myDataIn;
}

boolean getBit(byte myVarIn, byte whatBit) {
  boolean bitState;
  bitState = myVarIn & (1 << (whatBit-1));
  return bitState;
}

boolean Button(byte b) {
  digitalWrite(latchPin,0);
  delayMicroseconds(20);
  digitalWrite(latchPin,1);
  switchVar1 = shiftIn(dataPin, clockPin);
  delayMicroseconds(20);
  return getBit(switchVar1, b);
}

void sdCard() {
  lcd.clear();
  int i=0; int j=0;
  String files [1][3];
  File root; File entry;
  root = SD.open("/");
  do {
    entry =  root.openNextFile();
    lcd.setCursor((j*10)+1, i);
    String ename=entry.name();
    files[j][i]=ename;
    lcd.print(ename.substring(0,ename.indexOf(".")));
    i = i + 1;
    if ((i==4)&&(j==0)) {j=1; i=0;}
  } while ((entry)&&(i+j<5));
  i=0; j=0;
  while (mode==2) {
    lcd.setCursor(j*10, i);
    lcd.print((char)0);     
    if (Button(btnZDown)) {
      while (Button(btnZDown)) {}
      lcd.setCursor(j*10,i);
      lcd.print(" ");
      i = i + 1;
      if (i==4) {
        if (j==1) j=0; else j=1;
        i=0;
      }
      if (files[j][i]=="") {i=0; j=0;}
    }
    if (Button(btnReverse)) {
      while (Button(btnReverse)) {}
      mode=0;
    }
  }
}

void loop() {
  if (Button(btnZDown)) {
    while (Button(btnZDown)) {}
    move_menu_cursor(1);
  }
  if (Button(btnZUp)) {
    while (Button(btnZUp)) {}
    move_menu_cursor(2);
  }
  if (Button(btnReverse)) {
    while (Button(btnReverse)) {}
    mode=menu_cursor;
    if (mode==1) togglemode();
    if (mode==2) sdCard();
    if (mode==3) serialRepeater();
    lcd.clear();
    mainmenu();
  }
}

Grumpy_Mike

When you include code can you include a link to the libraries you used otherwise no one else can compile the code.

AllanB

Oh, you are of course missing ShiftRegLCD.h. It's makes it possible to run LCD's with only two digital pins.

Info here: http://code.google.com/p/arduinoshiftreglcd/

Code here: http://arduinoshiftreglcd.googlecode.com/files/ShiftRegLCD_2012.02.15.tar.gz

Go Up