Delay entfernt, cpu auf 160MHz, Ton gerichtet

This commit is contained in:
nimeissn 2022-07-13 20:45:08 +02:00
parent 682a8345ab
commit b0405eef7c
2 changed files with 180 additions and 55 deletions

View file

@ -16,3 +16,7 @@ lib_deps =
adafruit/Adafruit WS2801 Library@^1.1.1 adafruit/Adafruit WS2801 Library@^1.1.1
lbernstone/Tone32@^1.0.0 lbernstone/Tone32@^1.0.0
platform = espressif32 platform = espressif32
; set frequency to 160MHz
board_build.f_cpu = 160000000L

View file

@ -1,6 +1,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <Adafruit_WS2801.h> #include <Adafruit_WS2801.h>
#include <Tone32.h> #include <Tone32.h>
#include <Adafruit_NeoPixel.h>
/** /**
* Player 1: x = 0 * Player 1: x = 0
@ -11,6 +12,7 @@
#define PIN_JOYSTICK_1_BUTTON 12 #define PIN_JOYSTICK_1_BUTTON 12
#define PIN_JOYSTICK_2_BUTTON 26 #define PIN_JOYSTICK_2_BUTTON 26
#define PIN_PIEZO 17 #define PIN_PIEZO 17
#define CHANNEL_PIEZO 1
#define PIN_JOYSTICK_1_Y 13 #define PIN_JOYSTICK_1_Y 13
#define PIN_JOYSTICK_2_Y 27 #define PIN_JOYSTICK_2_Y 27
#define PIN_JOYSTICK_1_X 14 #define PIN_JOYSTICK_1_X 14
@ -54,7 +56,15 @@
#define GAME_STATE_RUNNING 1 #define GAME_STATE_RUNNING 1
#define GAME_STATE_END 2 #define GAME_STATE_END 2
#define GAME_STATE_INIT 3 #define GAME_STATE_INIT 3
#include <Adafruit_NeoPixel.h> #define GAME_STATE_INACTIVE 4
#define GAME_STATE_PLAY_WITH_ME 5
#define LIGHT_STATE_BAGGROUND 1
#define LIGHT_STATE_PLAY 2
#define LIGHT_STATE_WITH 3
#define LIGHT_STATE_ME 4
#define LIGHT_STATE_END 5
uint8_t dataPin = 2; uint8_t dataPin = 2;
uint8_t clockPin = 4; uint8_t clockPin = 4;
@ -89,10 +99,12 @@ void toggleLed(byte x, byte y, byte type);
byte getPlayerMovement(byte playerId); byte getPlayerMovement(byte playerId);
void endGame(); void endGame();
void attentionLight(); void attentionLight();
void attentionLight1();
unsigned long delay_light = millis();
byte lightState = LIGHT_STATE_BAGGROUND;
boolean delay_light_high = true;
#include <Arduino.h>
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) { static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
} }
@ -128,6 +140,8 @@ static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
void setup() void setup()
{ {
ledcAttachPin(PIN_PIEZO , CHANNEL_PIEZO);
ledcSetup(CHANNEL_PIEZO, 10000, 16);
pinMode(PIN_JOYSTICK_1_Y, INPUT); pinMode(PIN_JOYSTICK_1_Y, INPUT);
pinMode(PIN_JOYSTICK_1_BUTTON, INPUT_PULLUP); pinMode(PIN_JOYSTICK_1_BUTTON, INPUT_PULLUP);
pinMode(PIN_JOYSTICK_2_Y, INPUT); pinMode(PIN_JOYSTICK_2_Y, INPUT);
@ -147,6 +161,8 @@ void loop()
switch(gameState) { switch(gameState) {
case GAME_STATE_INIT: case GAME_STATE_INIT:
lightState = LIGHT_STATE_BAGGROUND;
delay_light_high = true;
initGame(); initGame();
break; break;
case GAME_STATE_RUNNING: case GAME_STATE_RUNNING:
@ -157,13 +173,33 @@ void loop()
if (isButtonPressed()) { if (isButtonPressed()) {
gameState = GAME_STATE_INIT; gameState = GAME_STATE_INIT;
} }
else if (millis() - lastDrawUpdate > 30000) else if (millis() - lastDrawUpdate > 3000 && millis() - lastDrawUpdate < 30000 && millis() - delay_light >= 500 && false == delay_light_high || millis() - lastDrawUpdate > 15000 && millis() - lastDrawUpdate < 30000 && millis() - delay_light >= 500 && true == delay_light_high )
{ {
gameState = GAME_STATE_INACTIVE;
}
else if (millis() - lastDrawUpdate > 30000 && millis() - delay_light >= 1500)
{
gameState = GAME_STATE_PLAY_WITH_ME;
}
break;
case GAME_STATE_INACTIVE:
resetLEDs();
pointsPlayerBlue = 0;
pointsPlayerRed = 0;
attentionLight1();
gameState = GAME_STATE_END;
break;
case GAME_STATE_PLAY_WITH_ME:
resetLEDs(); resetLEDs();
pointsPlayerBlue = 0; pointsPlayerBlue = 0;
pointsPlayerRed = 0; pointsPlayerRed = 0;
attentionLight(); attentionLight();
} gameState = GAME_STATE_END;
break; break;
} }
} }
@ -484,7 +520,9 @@ void attentionLight()
int with [46] = {13,14,15,16,17,18,22,36,42,58,57,56,55,54,53,73,74,75,76,77,78,93,106,114,126,133,115,116,117,118,113,154,155,156,157,158,153,163,176,183,193,195,196,197,198,194}; int with [46] = {13,14,15,16,17,18,22,36,42,58,57,56,55,54,53,73,74,75,76,77,78,93,106,114,126,133,115,116,117,118,113,154,155,156,157,158,153,163,176,183,193,195,196,197,198,194};
int me [30] = {53,54,55,56,57,58,65,75,85,93,94,95,96,97,98,113,114,115,116,117,118,126,138,146,121,133,144,124,135,141}; int me [30] = {53,54,55,56,57,58,65,75,85,93,94,95,96,97,98,113,114,115,116,117,118,126,138,146,121,133,144,124,135,141};
switch (lightState)
{
case LIGHT_STATE_BAGGROUND:
for (int i = 0; i< 200; i++) for (int i = 0; i< 200; i++)
{ {
@ -492,7 +530,12 @@ void attentionLight()
} }
pixels.show(); pixels.show();
delay(1000); lightState = LIGHT_STATE_PLAY;
delay_light = millis();
break;
case LIGHT_STATE_PLAY:
for (int i = 0; i<44; i++) for (int i = 0; i<44; i++)
{ {
@ -500,7 +543,11 @@ void attentionLight()
} }
pixels.show(); pixels.show();
delay(1500); lightState = LIGHT_STATE_WITH;
delay_light = millis();
break;
case LIGHT_STATE_WITH:
for (int i = 0; i< 200; i++) for (int i = 0; i< 200; i++)
{ {
@ -514,8 +561,11 @@ void attentionLight()
} }
pixels.show(); pixels.show();
delay(1500); lightState = LIGHT_STATE_ME;
delay_light = millis();
break;
case LIGHT_STATE_ME:
for (int i = 0; i< 200; i++) for (int i = 0; i< 200; i++)
{ {
@ -529,9 +579,80 @@ void attentionLight()
} }
pixels.show(); pixels.show();
delay(1500);
lightState = LIGHT_STATE_END;
delay_light = millis();
break;
case LIGHT_STATE_END:
lastDrawUpdate = millis(); lastDrawUpdate = millis();
resetLEDs(); resetLEDs();
lightState = LIGHT_STATE_BAGGROUND;
default:
lastDrawUpdate = millis();
lightState = LIGHT_STATE_BAGGROUND;
break;
}
} }
void attentionLight1()
{
byte r = random(1,255);
byte g = random(1,255);
byte b = random(1,255);
byte n = random(199);
pixels.setPixelColor(n,r,g,b);
byte r1 = random(1,255);
byte g1 = random(1,255);
byte b1 = random(1,255);
byte n1 = random(199);
pixels.setPixelColor(n1,r1,g1,b1);
pixels.show();
delay_light = millis();
delay_light_high = false;
}
bool is_waiting_for_ms(unsigned long wait_time_ms)
{
static bool is_waiting = false;
static unsigned long start_ms = 0;
static unsigned long static_wait_time_ms = 0;
if (false == is_waiting)
{
start_ms = millis();
is_waiting = true;
static_wait_time_ms = wait_time_ms;
}
else
{
if (millis() - static_wait_time_ms >= start_ms )
{
is_waiting = false;
}
}
return is_waiting;
}