Hi I have to combine these 2 codes but it doesn't do what it should do.
Can anyone help, what am I doing wrong? :)))
If you push the button in code 1 the rgb light will go on and a countdown is starting and it will count the times you push the button.
By combining the codes there should play a song from the mp3 player + the things in code 1.
In my attempt the mp3 player will play a song but the buttonpushcounter, countdown and the rgb light aren't working.
Code1
#include <Adafruit_NeoPixel.h>
#include "CountDown.h"
#include <U8g2lib.h>
#define PIN 6
#define LED_COUNT 10
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
CountDown CD;
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,SCL,SDA,U8X8_PIN_NONE);
const int buttonPin = 7; // the pin that the pushbutton is attached to
const int ledPin = 6; // the pin that the LED is attached to
const int buttonPin2 = 5;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int buttonPushCounter2 = 0; // counter for the number of button presses
int buttonState2 = 0; // current state of the button
int lastButtonState2 = 0;
void setup() {
// mp3
// alles voor de countdown en led
Serial.begin(9600);
Serial.println(__FILE__);
Serial.print("COUNTDOWN_LIB_VERSION: ");
Serial.println(COUNTDOWN_LIB_VERSION);
CD.start(0, 0, 2, 0);
u8g2.begin();
// alles voor de buttoncounter + rgb
pinMode(buttonPin, INPUT);
pinMode (buttonPin2, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
leds.begin();
leds.show();
}
void start()
{
}
void Red() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(0, leds.Color(255, 0, 0));// Moderately bright red color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Red2() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(1, leds.Color(255, 0, 0)); // Moderately bright red color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Red3() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(2, leds.Color(255, 0, 0)); // Moderately bright red color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Orange() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(3, leds.Color(255,50,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Orange2() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(4, leds.Color(255,50,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Orange3() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(5, leds.Color(255,50,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Orange4() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(6, leds.Color(255,50,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Green() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(7, leds.Color(0,255,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
// }
delay(5);
}
void Green2() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(8, leds.Color(0,255,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
// }
delay(5);
}
void Green3() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(9, leds.Color(0,255,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
// }
delay(5);
}
void UIT() {
for(int i=0;i<LED_COUNT;i++){
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(i, leds.Color(0,0,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
}
delay(5);
}
void Paars() {
for(int i=0;i<LED_COUNT;i++){
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(i, leds.Color(255,0,100)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
}
delay(5);
}
void loop() {
//mp3
//led begin
if (buttonPushCounter == 0) {
u8g2.setFont(u8g2_font_helvB12_tr);
u8g2.setCursor(0,50);
u8g2.print("Press to start");
u8g2.sendBuffer();
delay(100);
}
//countdown
if (buttonPushCounter > 1){
static uint32_t last_remaining = 0;
if (last_remaining != CD.remaining() || CD.remaining() == 0 )
{
Serial.println();
last_remaining = CD.remaining();
}
Serial.print('\t');
Serial.print(CD.remaining());
delay(250);
}
u8g2.clearBuffer();
if (buttonPushCounter > 1) {
u8g2.setFont(u8g2_font_helvB18_tr);
u8g2.setCursor(0,50);
u8g2.print(CD.remaining());
u8g2.sendBuffer();
Serial.print(CD.remaining());
Serial.println();
delay(750);
}
if (buttonPushCounter2 > 10) {
CD.stop();
}
//Rgb shit
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} if (buttonState == LOW) {
Serial.println("off");
}
}
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 != lastButtonState2) {
if (buttonState2 == HIGH) {
buttonPushCounter2++;
Serial.println("on");
Serial.print("number of button pushes2: ");
Serial.println(buttonPushCounter2);
} if (buttonState2 == LOW) {
Serial.println("off");
}
}
delay(50);
{
lastButtonState = buttonState;
}
{
lastButtonState2 = buttonState2;
}
/////////////////////////////////////////////////////
if (buttonPushCounter > 1) {
Red();
}
if (buttonPushCounter > 2) {
Red2();
}
if (buttonPushCounter > 3) {
Red3();
}
if (buttonPushCounter > 4) {
Orange();}
if (buttonPushCounter > 5) {
Orange2();}
if (buttonPushCounter > 6) {
Orange3();}
if (buttonPushCounter > 7) {
Orange4();}
if (buttonPushCounter2 > 8) {
Green();}
if (buttonPushCounter2 > 9) {
Green2();}
if (buttonPushCounter2 > 10) {
Green3();}
// countdown einde
if (buttonPushCounter2 > 10) {
u8g2.setFont(u8g2_font_helvB12_tr);
u8g2.setCursor(0,20);
u8g2.print("Good job!");
u8g2.sendBuffer();
delay(100);
}
//mp3
}
code2:
#include "WT2003S_Player.h"
#include <SoftwareSerial.h>
SoftwareSerial SSerial(2, 3); // RX, TX
#define COMSerial SSerial
WT2003S<SoftwareSerial> Mp3Player;
const int BUTTONPIN = 7;
int buttonState = 0;
boolean playing = false;
void setup() {
COMSerial.begin(9600);
Mp3Player.init(COMSerial);
pinMode(BUTTONPIN, INPUT);
}
void loop() {
if(playing==false){
Mp3Player.playSDRootSong(6);
Mp3Player.volume(20);
playing=true;
}
buttonState = digitalRead(BUTTONPIN);
if (buttonState == 1) {
Mp3Player.next();
}
}
My attempt to combine the codes:
#include <Adafruit_NeoPixel.h>
#include "CountDown.h"
#include <U8g2lib.h>
#include "WT2003S_Player.h"
#include <SoftwareSerial.h>
#define PIN 6
#define LED_COUNT 10
#define COMSerial SSerial
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
CountDown CD;
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,SCL,SDA,U8X8_PIN_NONE);
WT2003S<SoftwareSerial> Mp3Player;
SoftwareSerial SSerial(2, 3); // RX, TX
boolean playing = false;
const int buttonPin = 7; // the pin that the pushbutton is attached to
const int ledPin = 6; // the pin that the LED is attached to
const int buttonPin2 = 5;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int buttonPushCounter2 = 0; // counter for the number of button presses
int buttonState2 = 0; // current state of the button
int lastButtonState2 = 0;
void setup() {
// mp3
{
COMSerial.begin(9600);
Mp3Player.init(COMSerial);
pinMode(BUTTONPIN, INPUT);
}
// alles voor de countdown en led
Serial.begin(9600);
Serial.println(__FILE__);
Serial.print("COUNTDOWN_LIB_VERSION: ");
Serial.println(COUNTDOWN_LIB_VERSION);
CD.start(0, 0, 2, 0);
u8g2.begin();
// alles voor de buttoncounter + rgb
pinMode(buttonPin, INPUT);
pinMode (buttonPin2, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
leds.begin();
leds.show();
}
void start()
{
}
void Red() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(0, leds.Color(255, 0, 0));// Moderately bright red color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Red2() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(1, leds.Color(255, 0, 0)); // Moderately bright red color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Red3() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(2, leds.Color(255, 0, 0)); // Moderately bright red color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Orange() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(3, leds.Color(255,50,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Orange2() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(4, leds.Color(255,50,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Orange3() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(5, leds.Color(255,50,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Orange4() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(6, leds.Color(255,50,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
delay(5);
}
void Green() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(7, leds.Color(0,255,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
// }
delay(5);
}
void Green2() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(8, leds.Color(0,255,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
// }
delay(5);
}
void Green3() {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(9, leds.Color(0,255,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
// }
delay(5);
}
void UIT() {
for(int i=0;i<LED_COUNT;i++){
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(i, leds.Color(0,0,0)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
}
delay(5);
}
void Paars() {
for(int i=0;i<LED_COUNT;i++){
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
leds.setPixelColor(i, leds.Color(255,0,100)); // Moderately bright green color.
leds.show(); // This sends the updated pixel color to the hardware.
}
delay(5);
}
void loop() {
//mp3
if(playing==false){
Mp3Player.playSDRootSong(6);
Mp3Player.volume(20);
playing=true;
}
//led begin
if (buttonPushCounter == 0) {
u8g2.setFont(u8g2_font_helvB12_tr);
u8g2.setCursor(0,50);
u8g2.print("Press to start");
u8g2.sendBuffer();
delay(100);
}
//countdown
if (buttonPushCounter > 1){
static uint32_t last_remaining = 0;
if (last_remaining != CD.remaining() || CD.remaining() == 0 )
{
Serial.println();
last_remaining = CD.remaining();
}
Serial.print('\t');
Serial.print(CD.remaining());
delay(250);
}
u8g2.clearBuffer();
if (buttonPushCounter > 1) {
u8g2.setFont(u8g2_font_helvB18_tr);
u8g2.setCursor(0,50);
u8g2.print(CD.remaining());
u8g2.sendBuffer();
Serial.print(CD.remaining());
Serial.println();
delay(750);
}
if (buttonPushCounter2 > 10) {
CD.stop();
}
//Rgb shit
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} if (buttonState == LOW) {
Serial.println("off");
}
}
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 != lastButtonState2) {
if (buttonState2 == HIGH) {
buttonPushCounter2++;
Serial.println("on");
Serial.print("number of button pushes2: ");
Serial.println(buttonPushCounter2);
} if (buttonState2 == LOW) {
Serial.println("off");
}
}
delay(50);
{
lastButtonState = buttonState;
}
{
lastButtonState2 = buttonState2;
}
/////////////////////////////////////////////////////
if (buttonPushCounter > 1) {
Red();
}
if (buttonPushCounter > 2) {
Red2();
}
if (buttonPushCounter > 3) {
Red3();
}
if (buttonPushCounter > 4) {
Orange();}
if (buttonPushCounter > 5) {
Orange2();}
if (buttonPushCounter > 6) {
Orange3();}
if (buttonPushCounter > 7) {
Orange4();}
if (buttonPushCounter2 > 8) {
Green();}
if (buttonPushCounter2 > 9) {
Green2();}
if (buttonPushCounter2 > 10) {
Green3();}
// countdown einde
if (buttonPushCounter2 > 10) {
u8g2.setFont(u8g2_font_helvB12_tr);
u8g2.setCursor(0,20);
u8g2.print("Good job!");
u8g2.sendBuffer();
delay(100);
}
//mp3
if (buttonState == 1) {
Mp3Player.next();
}
}```