Hey, me again. i am trying to use the same button to play and pause a serial player. there are lots of similar posts, but almost all of them use a variable to toggle something high or low, and here i need to use different functions. i did most of the work, i think, based on some other examples. now only the pause works, it seems to me the bool is not switching states. I'm sure it is some simple logic mistake, but to be quite frank i am still incredibly bad at it (i am a puppet stop motion animator, it doesn't get more analogous than that...) . i would love if someone could point me to the probably obvious problem in the code.
Thank in advance!!!
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define LED_PIN 3
#define LED_COUNT 56
#define BRIGHTNESS 50 // Set BRIGHTNESS to about 1/5 (max = 255)
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
#include <SoftwareSerial.h>
#include "RedMP3.h"
#define MP3_RX 12//RX of Serial MP3 module connect to D7 of Arduino
#define MP3_TX 13//TX to D8, note that D8 can not be used as RX on Mega2560, you should modify this if you donot use Arduino UNO
MP3 mp3(MP3_RX, MP3_TX);
//int volume = 0;//0~0x1e (30 adjustable level)
//int8_t fileName = 01; // prefix of file name must be 001xxx 002xxx 003xxx 004xxx ...
int lastVolume = 0 ;
int folder = 01;//folder name must be 01 02 03 04 ...
int song = 1;
int trackall = 10;
int playstate = 0;
int lastplayState = 0;
int skip = 0;
int volume = 0;
bool pause = false;
// Define the digital inputs
#define DOWN A2
#define UP A3
#define LEFT A0
#define RIGHT A1
#define red_Button 9
#define yellow_Button 8
#define purple_Button 10
#define volume_Pot A10
#define play_Pause 4
#define Skip 7
#define robot_Switch 2
#define power_Switch A5
RF24 radio(53, 40); // nRF24L01 (CE, CSN)
const byte address[6] = "00001"; // Address
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte down;
byte up;
byte left;
byte right;
byte red;
byte yellow;
byte purple;
byte robot;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
// Define the radio communication
radio.begin();
radio.openWritingPipe(address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
// Activate the Arduino internal pull-up resistors
pinMode(DOWN, INPUT_PULLUP);
pinMode(UP, INPUT_PULLUP);
pinMode(LEFT, INPUT_PULLUP);
pinMode(RIGHT, INPUT_PULLUP);
pinMode(red_Button, INPUT_PULLUP);
pinMode(yellow_Button, INPUT_PULLUP);
pinMode(purple_Button, INPUT_PULLUP);
pinMode(play_Pause, INPUT_PULLUP);
pinMode(Skip, INPUT_PULLUP);
pinMode(robot_Switch, INPUT_PULLUP);
pinMode(power_Switch, OUTPUT);
// Set initial default values
data.down = 1; // Values from 0 to 255. When Joystick is in resting position, the value is in the middle, or 127. We actually map the pot value from 0 to 1023 to 0 to 255 because that's one BYTE value
data.up = 1;
data.left = 1;
data.right = 1;
data.red = 1;
data.yellow = 1;
data.purple = 1;
data.robot = 1;
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(BRIGHTNESS);
digitalWrite(power_Switch, HIGH);// power LED indicator ON
delay(500);
}
void loop() {
// Read all digital inputs
data.down = digitalRead(DOWN);
data.up = digitalRead(UP);
data.left = digitalRead(LEFT);
data.right = digitalRead(RIGHT);
data.red = digitalRead(red_Button);
data.yellow = digitalRead(yellow_Button);
data.purple = digitalRead(purple_Button);
data.robot = digitalRead(robot_Switch);
if (data.yellow == LOW) {
strip.fill(strip.Color(255, 200 , 0), 0, 56);
strip.show();
}
else if (data.purple == LOW) {
strip.fill(strip.Color(128, 0 , 128), 0, 56);
strip.show();
}
else if (data.red == LOW) {
strip.fill(strip.Color(255, 0 , 0), 0, 56);
strip.show();
}
volume = map(analogRead(volume_Pot), 0, 1024, 0, 16);
if (volume != lastVolume) {
mp3.setVolume(volume);
lastVolume = volume;
//Serial.print("pot set to: "); Serial.println (analogRead(volume_Pot));
//Serial.print("volume set to: "); Serial.println (volume);
}
playstate = digitalRead(play_Pause);
if (playstate == LOW) {
pause = (!pause);
if (pause = true) {
delay(100);//you should wait for >=50ms between two commands
mp3.pause();
delay(100);
Serial.println("pause_");
}
else {
delay(100);//you should wait for >=50ms between two commands
mp3.play();
Serial.println("play_");
delay(100);
}
}
skip = digitalRead(Skip);
if (skip == LOW) {
delay(50);
mp3.playWithFileName(folder, song);
delay(50);
// mp3.nextSong();
if (song == trackall) {
song = 1;
}
else {
song = song + 1;
}
delay(100);
}
// Serial.print("playing track #"); Serial.println(song);
//Serial.print("down_");
//Serial.println(data.down);
//Serial.print("up_");
//Serial.println(data.up);
//Serial.print("left_");
//Serial.println(data.left);
//Serial.print("right_");
//Serial.println(data.right);
//Serial.print("red_");
//Serial.println(data.red);
//Serial.print("yellow_");
//Serial.println(data.yellow);
//Serial.print("purple_");
//Serial.println(data.purple);
//Serial.print("robot_");
//Serial.println(data.robot);
// Send the whole data from the structure to the receiver
radio.write(&data, sizeof(Data_Package));
}