77Views1Replies

Author Options:

Trying to make a arduino based photo booth, and need help with my code for the countdown to be triggered by a button? Answered

i am new to Arduino and am trying to make an Arduino based Photo Booth which will have a button that is pressed and triggers a countdown on an 8x8 SPI matrix and at the end of the countdown it triggers a SLR Camera (Nikon D5500) (i still need to work out how to trigger the camera and write the camera trigger code).

at the moment i have managed to create code that starts the countdown when a button is pressed however it seems to only want to listen to the button randomly and triggers whenever it seems to feel like it.

Here is the code i have so far...Have i written it correctly?



#include "LedControl.h"

const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

LedControl lc=LedControl(12,11,10,1);  // Pins: DIN,CLK,CS, # of Display connected

unsigned long delayTime=1000;  // Delay between Frames

// Put values in arrays

byte invader1a[] =
{
  B01111110, // #9
  B11111111,
  B11100111,
  B11111111,
  B01111111,
  B00000111,
  B11111111,
  B01111110
};

byte invader1b[] =
{
  B01111110, // #8
  B11111111,
  B11100111,
  B01111110,
  B01111110,
  B11100111,
  B11111111,
  B01111110
};

byte invader1c[] =
{
  B11111111, // #7
  B11111111,
  B00000111,
  B00001110,
  B00011100,
  B00111000,
  B00111000,
  B00111000
};

byte invader1d[] =
{
  B01111110, // #6
  B11111111,
  B11100000,
  B11111110,
  B11111111,
  B11100111,
  B11111111,
  B01111110
};

byte invader1e[] =
{
  B11111111, // #5
  B11111111,
  B11100000,
  B11111110,
  B11111111,
  B00000111,
  B11111111,
  B01111110
};

byte invader1f[] =
{
  B11101110, // #4
  B11101110,
  B11101110,
  B11111111,
  B11111111,
  B00001110,
  B00001110,
  B00001110
};


byte invader1g[] =
{
  B11111110, // #3
  B11111111,
  B00000111,
  B11111110,
  B11111110,
  B00000111,
  B11111111,
  B11111110
};

byte invader1h[] =
{
  B01111110, // #2
  B11111111,
  B11100111,
  B00001110,
  B00111000,
  B11100000,
  B11111111,
  B11111111
};


byte invader1i[] =
{
  B00011100, // #1
  B00111100,
  B01111100,
  B00011100,
  B00011100,
  B00011100,
  B00011100,
  B00011100
};

byte invader1j[] =
{
  B00000000, // #smile
  B00000000,
  B00100100,
  B00000000,
  B10000001,
  B01000010,
  B00111100,
  B00000000
};

byte invader1k[] =
{
  B00000000, // #blank
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000
};
void setup()
{
  lc.shutdown(0,false);  // Wake up displays
  lc.shutdown(1,false);
  lc.setIntensity(0,5);  // Set intensity levels
  lc.setIntensity(1,5);
  lc.clearDisplay(0);  // Clear Displays
  lc.clearDisplay(1);
  pinMode(buttonPin, INPUT);
}


//  Take values in Arrays and Display them
void sinvader1a()
{
  for (int i = 0; i < 8; i++) 
  {
    lc.setRow(0,i,invader1a[i]);
  }
}

void sinvader1b()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1b[i]);
  }
}

void sinvader1c()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1c[i]);
  }
}

void sinvader1d()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1d[i]);
  }
}

void sinvader1e()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1e[i]);
  }
}
void sinvader1f()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1f[i]);
  }
}
void sinvader1g()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1g[i]);
  }
}
void sinvader1h()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1h[i]);
  }
}

void sinvader1i()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1i[i]);
  }
}

void sinvader1j()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1j[i]);
  }
}

void sinvader1k()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0,i,invader1k[i]);
  }
}


void loop()
{// read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    sinvader1a();
    delay(delayTime);
    sinvader1b();
    delay(delayTime);
    sinvader1c();
    delay(delayTime);
    sinvader1d();
    delay(delayTime);
    sinvader1e();
    delay(delayTime);
    sinvader1f();
    delay(delayTime);
    sinvader1g();
    delay(delayTime);
    sinvader1h();
    delay(delayTime);
    sinvader1i();
    delay(delayTime);
    sinvader1j();
    delay(delayTime);
    sinvader1k();
  }
if (buttonState == LOW) {
   buttonState = digitalRead(buttonPin);
  }  

}

Discussions

0
None
steveastrouk

3 years ago

Put your LED table in a two dimensional array, each row stores 8 bytes, and you need 10 of them, then your code becomes

//define a 2D array for all your digits

digitholder [9] [7]

//^^^^^^^look up the structure for a 2 D array definition}

//Now your main code is simply

for (byte digit=0,digit<=9,digit--)

for (byte ctr=0,ctr<=7,ctr++)

lc.setRow(0,ctr,digitholder[digit] [ctr]);

delay(delaytime)

How is your switch wired ?