i am new to arduino and am working om a school project at the moment.
i want to make a (sort of) lootbox where you need to push a button and when to push it that it wil freeze the led where it is standing on.
at the moment i only have the progamme for the loop.
and have tryed some other stuf but none of them seems to work.
it would mean a lot if you guys could help me out on this one.
Roydegoedste:
i am new to arduino and am working om a school project at the moment.
i want to make a (sort of) lootbox where you need to push a button and when to push it that it wil freeze the led where it is standing on.
at the moment i only have the progamme for the loop.
and have tryed some other stuf but none of them seems to work.
it would mean a lot if you guys could help me out on this one.
(down below is the loop programme)
Hello, Roy and welcome to the Arduino forum.
I know it's hard, but, you can't "stop" the loop. What you can do is stop what it is doing. When you have tested the IDE example programs using a LED and using a switch, you can combine the logic you find there into a program to do your assignment.
See if you can make sense of this and if it helps. (Compiles ok but not tried...)
This will freeze the LED while the button is pressed. If you need the cycle frozen with a press and then restart with another press, it's a minor change the code...
#define NUM_LEDS 6
const int pinArray[NUM_LEDS] = {2, 3, 4, 5, 6, 7};
const int pinStopButton = 9;
//depending on how your LEDs are wired, you may need to reverse these
#define LED_ON HIGH
#define LED_OFF LOW
//button reading happens in two passes
//for debouncing
#define FIRST_PASS 0 //name of first pass
#define SECOND_PASS 1 //name of second pass
#define BTN_CHECK_TIME 10 //interval in mS between passes
//LED chaser is done with a simple state machine
//on and off times are LED_TIME_DELAY mS
#define LED_PULSE_ON 0
#define LED_PULSE_OFF 1
#define LED_TIME_DELAY 50
int
count = 0;
bool
freezeLed = false;
void setup()
{
//set button pin as input, mode INPUT_PULLUP
//pressing button should "ground" it (reads LOW)
pinMode( pinStopButton, INPUT_PULLUP );
// we make all the declarations at once
for (count=0;count<NUM_LEDS;count++)
{
pinMode(pinArray[count], OUTPUT);
digitalWrite( pinArray[count], LED_OFF );
}//for
}//setup
void loop()
{
//main loop just calls button read and LED logic super-fast
//the timing is done within these two routines
//if it's not time for something to happen yet, they just return
DoButton();
DoLEDs();
}//loop
void DoButton( void )
{
int
stateButton;
static byte
pass = 0;
static int
laststateButton;
static unsigned long
timeButton = millis();
unsigned long
timeNow;
//get the millisecond count value "now"
timeNow = millis();
//see if enough time has elapsed to do a button read
if( (timeNow - timeButton) < BTN_CHECK_TIME )
return;
//time for a read; set timeButton up for next button read
timeButton = timeNow;
//pass tracks if we're on the first read or second read of a button
switch( pass )
{
case FIRST_PASS:
//the first pass establishes the button's state (high or low)
laststateButton = digitalRead( pinStopButton );
pass = SECOND_PASS;
break;
case SECOND_PASS:
//on the second pass, which happens BTN_CHECK_TIME after the first
//we read the button again
stateButton = digitalRead( pinStopButton );
//if it's the same as the first, we say we have a solid read
//(i.e. no bounce)
if( digitalRead( pinStopButton ) == laststateButton )
{
if( stateButton == HIGH )
{
//if button released (high) then the button has been
//released to release the freeze flag
freezeLed = false;
}//if
else
{
//if button pressed (low) then the button has been
//pressed; we want to freeze the current LED
//this flag tells the LED routine to do that
freezeLed = true;
}//else
}//if
//ready for the next button press
pass = FIRST_PASS;
break;
}//switch
}//DoButton
void DoLEDs( void )
{
static byte
count = 0;
static int
lastfreezeLed = 0,
freezeCount;
static byte
stateOnOff = LED_OFF;
unsigned long
timeNow;
static unsigned long
timeLED = millis();
//time between LED updates is LED_TIME_DELAY
timeNow = millis();
if( (timeNow - timeLED) < LED_TIME_DELAY )
return;
timeLED = timeNow;
//check to see if frozen (see button code above)
if( freezeLed == true )
{
if( lastfreezeLed != freezeLed )
{
//this is the first time here since the freeze flag was set
//grab the current count so we know which LED we're working with
freezeCount = count;
//turn the LED on
digitalWrite(pinArray[freezeCount], LED_ON);
//and set this variable so next time through we know we've done this part
lastfreezeLed = freezeLed;
}//if
}//if
else
{
//freeze is false if here
//first time through here with freeze false?
if( lastfreezeLed != freezeLed )
{
//yes; turn off frozen LED
digitalWrite(pinArray[freezeCount], LOW);
//and set this variable so next time through we know we've done this part
lastfreezeLed = freezeLed;
return;
}//if
//not the first time through so we cycle the LEDs in sequence
//stateOnOff is a variable that tells us if the LEDs should be turned
//on or off
//this could also be done
//if( stateOnOff == LED_PULSE_ON )
//{
// digitalWrite(pinArray...);
//
//but switch is cooler :)
switch( stateOnOff )
{
case LED_PULSE_ON:
digitalWrite(pinArray[count], HIGH);
stateOnOff = LED_PULSE_OFF;
break;
case LED_PULSE_OFF:
digitalWrite(pinArray[count], LOW);
stateOnOff = LED_PULSE_ON;
break;
}//switch
//as we cycle each LED off, bump to the next LED
//make sure we only count as high as the number of LEDs we have
count++;
if( count == NUM_LEDS )
count = 0;
}//if
}//DoLEDs