updated main.c
This commit is contained in:
76
src/main.cpp
76
src/main.cpp
@@ -1,16 +1,16 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include "buttons.h"
|
||||||
|
|
||||||
#define LED1 PB14
|
#define LED1 PB14
|
||||||
#define LED2 PB15
|
#define LED2 PB15
|
||||||
#define LED3 PA8
|
#define LED3 PA8
|
||||||
#define BUTTON PA0
|
#define BUTTON1 PA0
|
||||||
|
#define BUTTON2 PA1
|
||||||
|
|
||||||
|
|
||||||
#define BUTTONDELAY 500
|
|
||||||
|
|
||||||
|
|
||||||
uint8_t patternIndex = 0;
|
uint8_t patternIndex = 0;
|
||||||
unsigned long buttontimer = 0;
|
|
||||||
bool buttonFlag = false;
|
|
||||||
bool patternFlag = false;
|
bool patternFlag = false;
|
||||||
|
|
||||||
|
|
||||||
@@ -22,58 +22,70 @@ uint8_t ledpattern[5][3] = {
|
|||||||
{0,0,1}
|
{0,0,1}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int patternlength = sizeof(ledpattern)/sizeof(ledpattern[0]);
|
||||||
|
|
||||||
|
void nextPattern( void )
|
||||||
|
{
|
||||||
|
patternIndex++;
|
||||||
|
if(patternIndex > patternlength)
|
||||||
|
{
|
||||||
|
patternIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buttons button1(BUTTON1, 100,1000);
|
||||||
|
buttons button2(BUTTON2, 100,1000);
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(LED1, OUTPUT);
|
pinMode(LED1, OUTPUT);
|
||||||
pinMode(LED2, OUTPUT);
|
pinMode(LED2, OUTPUT);
|
||||||
pinMode(LED3, OUTPUT);
|
pinMode(LED3, OUTPUT);
|
||||||
pinMode(BUTTON, INPUT_PULLUP);
|
|
||||||
|
button1.begin();
|
||||||
|
button2.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
unsigned long currentMillis = millis();
|
button1.update();
|
||||||
//debounce button
|
button2.update();
|
||||||
if(!digitalRead(BUTTON))
|
|
||||||
{
|
|
||||||
if(!buttonFlag)
|
|
||||||
{
|
|
||||||
//button not detected jet, check timer
|
|
||||||
if(currentMillis - buttontimer >= BUTTONDELAY)
|
|
||||||
{
|
|
||||||
buttonFlag = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//button is not pressed, keep updating the timer
|
|
||||||
buttonFlag = false;
|
|
||||||
patternFlag = false;
|
|
||||||
buttontimer = millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(buttonFlag)
|
if(button1.state())
|
||||||
{
|
{
|
||||||
if(!patternFlag)
|
if(!patternFlag)
|
||||||
{
|
{
|
||||||
patternIndex++;
|
//button detected, increase pattern
|
||||||
if(patternIndex > 5)
|
nextPattern();
|
||||||
{
|
|
||||||
patternIndex = 0;
|
|
||||||
}
|
|
||||||
patternFlag = true;
|
patternFlag = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(button2.state())
|
||||||
|
{
|
||||||
|
if(!patternFlag)
|
||||||
|
{
|
||||||
|
//second input, skip a pattern
|
||||||
|
nextPattern();
|
||||||
|
nextPattern();
|
||||||
|
patternFlag = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(button1.state() | button2.state() )
|
||||||
|
{
|
||||||
//write pattern to the LEDs
|
//write pattern to the LEDs
|
||||||
digitalWrite(LED1,!ledpattern[patternIndex][0]);
|
digitalWrite(LED1,!ledpattern[patternIndex][0]);
|
||||||
digitalWrite(LED2,!ledpattern[patternIndex][1]);
|
digitalWrite(LED2,!ledpattern[patternIndex][1]);
|
||||||
digitalWrite(LED3,!ledpattern[patternIndex][2]);
|
digitalWrite(LED3,!ledpattern[patternIndex][2]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
//leds off
|
||||||
digitalWrite(LED1,1);
|
digitalWrite(LED1,1);
|
||||||
digitalWrite(LED2,1);
|
digitalWrite(LED2,1);
|
||||||
digitalWrite(LED3,1);
|
digitalWrite(LED3,1);
|
||||||
|
patternFlag = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user