button + counter

ey!

I have the following code in the loop of a program, which sends a bmp file to a Led strip.

void loop() {
  int buttonstate = digitalRead(buttonPin);

  if (buttonstate == HIGH) {
    counter ++;
    if (counter == 5) {
      counter = 1;
    }
  }

  if (counter == 1) {
    SendFile("set01.bmp");
  }
  else if (counter == 2) {
    SendFile("set02.bmp");
  }
  else if (counter == 3) {
    SendFile("set03.bmp");
  }
  else if (counter == 4) {
    SendFile("set04.bmp");
  }
}

It works correctly, because there are 4 files in the sd card. If there would be 5, the 5th would never be displayed, and if there would be 3, the program would get stucked at the 3rd file.

How could I write a code that works regardless the amount of files that there is in the sd card?

This would mean that if the program sends set01.bmp, the next one would be set02.bmp, the next one would be set03.bmp, and so on, and if the next one doesnt exist, it should send again set01.bmp

thanks!

I believe this is your problem right here...

if (buttonstate == HIGH) {
    counter ++;
    if (counter == 5) {
      counter = 1;
    }

If you're calling something like this:

else if (counter == 5) {
    SendFile("set04.bmp");
  }

Nothing will happen because you're setting counter to 1 whenever it hits 5.

I know which my problem is! I dont know how to sort it out!

I obviously cannot write this with numbers, because if I have numbers I have to respect also the amount of files available, but I dont know a way to do this without numbers.

I want to be able to change the amount of files without having to have a program for each specific amount of files. that is why I thought about a program that would work like this:

send set01.bmp
press button - send set02.bmp - if set02.bmp doesnt exist, send set01.bmp
press button - send set03.bmp - if set03.bmp doesnt exist, send set01.bmp
press button - send set04.bmp - if set04.bmp doesnt exist, send set01.bmp
press button - send set05.bmp - if set05.bmp doesnt exist, send set01.bmp
press button - send set06.bmp - if set06.bmp doesnt exist, send set01.bmp
etc...

the code is fine... just make the change from

if (counter == 5) {

  counter = 1;

}

to

if (counter == 6) {

  counter = 1;

}

If you want to add a 5th.

I dont want to change the code every time I change the amount of files that are present in the sd card. I would like to have a code that works always good, regardless how many files are in the sd card.

camilozk:
I dont want to change the code every time I change the amount of files that are present in the sd card. I would like to have a code that works always good, regardless how many files are in the sd card.

Well I don't know what to say then. I've never used an SD card with arduino so maybe find a way to read the amount of files on an SD card, and store it as a name...

byte amountOfSDfiles = // read amount of files

if (counter == amountOfSDfiles) {

  counter = 1;

}

yes I thought about it, but I havent find a code that detects how many files are in a sd card

what about this?

number = 1

send number.bmp
press button
send (number + 1).bmp
store "new number"
if "new number".bmp doesnt exist, send 1.bmp
store number = 1

camilozk:
what about this?

number = 1

send number.bmp
press button
send (number + 1).bmp
store "new number"
if "new number".bmp doesnt exist, send 1.bmp
store number = 1

Give it a try, and post results!

I will. I dont know how to write this.

I am looking how to store a number

camilozk:
I will. I dont know how to write this.

I am looking how to store a number

in very beginning...

 int newNumber = 0;

In your loop() -

 newNumber = // whatever

ok. I did this:

int numberFile = 01;


void loop() {
  int buttonstate = digitalRead(buttonPin);

  if (buttonstate == HIGH) {
    numberFile ++;
  }

    SendFile("numberFile.bmp");
}

but it doesnt work. Evidently the "numberFile" that appears in the order SendFile("numberFile.bmp"); is not being replaced by 01

Post the whole code. Also, you should wire your button so it's connected to GND on one end, and then to the Arduino pin on the other, and then in void setup do

 pinMode(button, INPUT_PULLUP);

then, when you're reading the button value remember that because of the change you made

HIGH = not pressed
LOW = pressed

Also,

"numberFile.bmp" is literally looking for a "sendfile.bmp" . You'd have to do something like ('numberFile'".bmp") , but I don't know exactly how to do that.

the button works as it is. the code as posted in #1 works perfect. the button is not the problem

#include <Adafruit_NeoPixel.h>
#include <SD.h>
#include <SPI.h>

#define SDssPin 4
int NPPin = 6;
int g = 0;
int b = 0;
int r = 0;

#define STRIP_LENGTH 3
int frameDelay = 10;
int menuItem = 1;
int initDelay = 0;
int repeat = 0;
int repeatDelay = 0;
int updateMode = 0;
int repeatTimes = 1;
int brightness = 99;

byte x;

int buttonPin = 2;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIP_LENGTH, NPPin, NEO_GRB + NEO_KHZ800);

File root;
File dataFile;
String m_CurrentFilename = "";
int m_FileIndex = 0;
int m_NumberOfFiles = 0;
long buffer[STRIP_LENGTH];

int numberFile = 01;


void setup() {
  setupLEDs();
  setupSDcard();
  pinMode(buttonPin, INPUT);
}


void loop() {
  int buttonstate = digitalRead(buttonPin);

  if (buttonstate == HIGH) {
    numberFile ++;
  }

    SendFile("numberFile.bmp");
}



void setupLEDs() {
  strip.begin();
  strip.show();
}


void setupSDcard() {
  pinMode(SDssPin, OUTPUT);
  while (!SD.begin(SDssPin)) {
  }
}


void SendFile(String Filename) {
  char temp[14];
  Filename.toCharArray(temp, 14);

  dataFile = SD.open(temp);

  if (dataFile) {
    ReadTheFile();
    dataFile.close();
  }
  else {
    delay(1000);
    setupSDcard();
    return;
  }
}


void latchanddelay(int dur) {
  strip.show();
  delay(dur);
}


void ClearStrip(int duration) {
  int x;
  for (x = 0; x < STRIP_LENGTH; x++) {
    strip.setPixelColor(x, 0);
  }
  strip.show();
}


uint32_t readLong() {
  uint32_t retValue;
  byte incomingbyte;

  incomingbyte = readByte();
  retValue = (uint32_t)((byte)incomingbyte);

  incomingbyte = readByte();
  retValue += (uint32_t)((byte)incomingbyte) << 8;

  incomingbyte = readByte();
  retValue += (uint32_t)((byte)incomingbyte) << 16;

  incomingbyte = readByte();
  retValue += (uint32_t)((byte)incomingbyte) << 24;

  return retValue;
}



uint16_t readInt() {
  byte incomingbyte;
  uint16_t retValue;

  incomingbyte = readByte();
  retValue += (uint16_t)((byte)incomingbyte);

  incomingbyte = readByte();
  retValue += (uint16_t)((byte)incomingbyte) << 8;

  return retValue;
}


int readByte() {
  int retbyte = -1;
  while (retbyte < 0) retbyte = dataFile.read();
  return retbyte;
}


void getRGBwithGamma() {
  g = gamma(readByte()) / (101 - brightness);
  b = gamma(readByte()) / (101 - brightness);
  r = gamma(readByte()) / (101 - brightness);
}


void ReadTheFile() {
#define MYBMP_BF_TYPE           0x4D42
#define MYBMP_BF_OFF_BITS       54
#define MYBMP_BI_SIZE           40
#define MYBMP_BI_RGB            0L
#define MYBMP_BI_RLE8           1L
#define MYBMP_BI_RLE4           2L
#define MYBMP_BI_BITFIELDS      3L

  uint16_t bmpType = readInt();
  uint32_t bmpSize = readLong();
  uint16_t bmpReserved1 = readInt();
  uint16_t bmpReserved2 = readInt();
  uint32_t bmpOffBits = readLong();
  bmpOffBits = 54;

  /* Check file header */
  if (bmpType != MYBMP_BF_TYPE || bmpOffBits != MYBMP_BF_OFF_BITS) {
    delay(1000);
    return;
  }

  /* Read info header */
  uint32_t imgSize = readLong();
  uint32_t imgWidth = readLong();
  uint32_t imgHeight = readLong();
  uint16_t imgPlanes = readInt();
  uint16_t imgBitCount = readInt();
  uint32_t imgCompression = readLong();
  uint32_t imgSizeImage = readLong();
  uint32_t imgXPelsPerMeter = readLong();
  uint32_t imgYPelsPerMeter = readLong();
  uint32_t imgClrUsed = readLong();
  uint32_t imgClrImportant = readLong();

  /* Check info header */
  if ( imgSize != MYBMP_BI_SIZE || imgWidth <= 0 ||
       imgHeight <= 0 || imgPlanes != 1 ||
       imgBitCount != 24 || imgCompression != MYBMP_BI_RGB ||
       imgSizeImage == 0 )
  {
    delay(1000);
    return;
  }

  int displayWidth = imgWidth;
  if (imgWidth > STRIP_LENGTH) {
    displayWidth = STRIP_LENGTH;
  }


  /* compute the line length */
  uint32_t lineLength = imgWidth * 3;
  if ((lineLength % 4) != 0)
    lineLength = (lineLength / 4 + 1) * 4;


  // Note:
  // The x,r,b,g sequence below might need to be changed if your strip is displaying
  // incorrect colors.  Some strips use an x,r,b,g sequence and some use x,r,g,b
  // Change the order if needed to make the colors correct.

  for (int y = imgHeight; y > 0; y--) {
    int bufpos = 0;
    for (int x = 0; x < displayWidth; x++) {
      uint32_t offset = (MYBMP_BF_OFF_BITS + (((y - 1) * lineLength) + (x * 3))) ;
      dataFile.seek(offset);

      getRGBwithGamma();

      strip.setPixelColor(x, r, b, g);

    }
    latchanddelay(frameDelay);
  }
}


PROGMEM const char gammaTable[]  = {
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,
  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,
  4,  4,  4,  4,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,  7,  7,
  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9, 10, 10, 10, 10, 11,
  11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16,
  16, 17, 17, 17, 18, 18, 18, 19, 19, 20, 20, 21, 21, 21, 22, 22,
  23, 23, 24, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30,
  30, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 37, 37, 38, 38, 39,
  40, 40, 41, 41, 42, 43, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50,
  50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 58, 58, 59, 60, 61, 62,
  62, 63, 64, 65, 66, 67, 67, 68, 69, 70, 71, 72, 73, 74, 74, 75,
  76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
  92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 104, 105, 106, 107, 108,
  109, 110, 111, 113, 114, 115, 116, 117, 118, 120, 121, 122, 123, 125, 126, 127
};


inline byte gamma(byte x) {
  return pgm_read_byte(&gammaTable[x]);
}

Okay... numberFile ++; should be numberFile++ , not sure if it makes a difference however.

Try this, add Serial.begin(9600); to void setup and under SendFile("numberFile.bmp"); , add

 Serial.println(numberFile);

to see if the counter you created is even counting in the first place. that's a start.

noup. that doesnt work.

When I write

SendFile("01.bmp");

it works

but when I write

SendFile("numberFile.bmp");

it doesnt

That means that it is not replacing numberFile with 01

camilozk:
noup. that doesnt work.

well then, even if the code below is right, which it isn't by the way, nothing will happen.

camilozk:
but when I write

SendFile("numberFile.bmp");

it doesnt

That means that it is not replacing numberFile with 01

that's because arduino is literally looking for a file called numberFile.bmp .

So you have two problems -

  1. Your counter isn't, well, counting.

  2. You need to fix that SendFile line of code. That I don't know how, but perhaps someone else would.

Let's work on one thing at a time, however. Do what I said in post #12, and then also:

Change

Serial.println(numberFile);

to

Serial.print("NumberFile: ");
Serial.print(numberFile); 
Serial.print"         buttonState: ");
Serial.println(buttonState);

Is the arduino recognizing the button? If so, you'll see 0's and 1's change after 'buttonState:' that should correspond with when you press the button.

I actually would like to fix first the fact that "numberFile" is not being replaced by "01" in the command

SendFile("numberFile.bmp");

because if I dont fix this, even if the counter works, I will never be able to see it running.

Regarding the arduino recognizing the button, as I said before, the button works fine. the code as posted in #1 works fine, which means that the button does it as well.

camilozk:
Regarding the arduino recognizing the button, as I said before, the button works fine. the code as posted in #1 works fine, which means that the button does it as well.

okay, well figure out what's different in your original code and this code that would make things different.

Again, I don't see what's wrong with my very first solution, because if you need to take the time to add a file to the SD card, it can't be much more work to change a single character in the code.

The SD library can only support 8+3 character file names. "numberfile" has more than 8.