So I'm working on a project for quite some time now. As you can see below I have an accelerometer that reacts to movements and turns the LEDs on/off according to those movements. I also have a push button to control the LEDs as well. The problem is, at the moment only the accelerometer can control the LEDs, the push button does nothing. I would like for the push button to work when the LEDs are out (so not active because of the accelerometer). Any advise?
int speakerPin = 2;
int inPin1 = 4; // choose the input pin (for a pushbutton)
int val1 = 0; // variable for reading the pin status
int ledPin1 = 7;
int ledPin2 = 8;
int ledPin3 = 9;
int ledPin4 = 10;
int ledPin5 = 11;
int ledPin6 = 12;
int inPin2 = 13; // choose the input pin (for a pushbutton)
int val2 = 0; // variable for reading the pin status
boolean isPushed1 = false; // variable for the status of the button
boolean isPressed1 = false; // variable for the status of the lights
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int an1, an2, an3 = 0;
int tempo = 300;
#include <Wire.h>
void setup()
{
pinMode(speakerPin, OUTPUT);
pinMode(inPin1, INPUT); // declare pushbutton as input
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(inPin2, INPUT); // declare pushbutton as input
Serial.begin(19200);
nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
nunchuck_init(); // write the initilization handshake
Serial.print ("Finished setup\n");
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
stopMuziek();
}
}
}
void loop(){
val1 = digitalRead(inPin1); // read input value
Serial.println(val1); // print the value of "value" on the Serial Monitor
if (val1 == HIGH) { // check if the input is HIGH (button pushed)
digitalWrite(speakerPin, HIGH); // turn LED ON
speelMuziek();
} else {
digitalWrite(speakerPin, LOW); // turn LED OFF
stopMuziek();
}
val2 = digitalRead(inPin2); // read input value
Serial.println(val2); // print the value of "value" on the Serial Monitor
if (val2 == HIGH) { // check if the input is HIGH (button pushed)
isPushed1 = true;
} else {
if(isPushed1){
if(isPressed1){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin6, LOW);
isPressed1 = false;
} else {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, HIGH);
isPressed1 = true;
}
isPushed1 = false;
} else {
}
}
nunchuck_get_data();
nunchuck_print_data();
delay(100);
}
void stopMuziek(){
noTone(5);
}
void speelMuziek(){
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
stopMuziek();
}
//
// Nunchuck functions
//
static uint8_t nunchuck_buf[6]; // array to store nunchuck data,
// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
DDRC |= _BV(pwrpin) | _BV(gndpin);
PORTC &=~ _BV(gndpin);
PORTC |= _BV(pwrpin);
delay(100); // wait for things to stabilize
}
// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
void nunchuck_init(){
Wire.begin(); // join i2c bus as master
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0x40); // writes memory address
Wire.write(0x00); // writes sent a zero.
Wire.endTransmission(); // stop transmitting
}
// write a request for data to the nunchuck
// was "write_zero()"
void nunchuck_write_request(){
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0x00); // writes one byte
Wire.endTransmission(); // stop transmitting
}
// Receive data back from the nunchuck,
int nunchuck_get_data(){
int cnt=0;
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ()) {
// receive byte as an integer
nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());
cnt++;
}
nunchuck_write_request(); // write request for next data payload
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
return 1; // success
}
return 0; //failure
}
// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits. That is why I
// multiply them by 2 * 2
void nunchuck_print_data(){
static int i=0;
int joy_x_axis = nunchuck_buf[0];
int joy_y_axis = nunchuck_buf[1];
int accel_x_axis = nunchuck_buf[2]; // * 2 * 2;
int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;
int endValue =(sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2)));
int z_button = 0;
int c_button = 0;
// byte nunchuck_buf[5] contains bits for z and c buttons
// it also contains the least significant bits for the accelerometer data
// so we have to check each bit of byte outbuf[5]
if ((nunchuck_buf[5] >> 0) & 1)
z_button = 1;
if ((nunchuck_buf[5] >> 1) & 1)
c_button = 1;
if ((nunchuck_buf[5] >> 2) & 1)
accel_x_axis += 2;
if ((nunchuck_buf[5] >> 3) & 1)
accel_x_axis += 1;
if ((nunchuck_buf[5] >> 4) & 1)
accel_y_axis += 2;
if ((nunchuck_buf[5] >> 5) & 1)
accel_y_axis += 1;
if ((nunchuck_buf[5] >> 6) & 1)
accel_z_axis += 2;
if ((nunchuck_buf[5] >> 7) & 1)
accel_z_axis += 1;
Serial.print(sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2)));
Serial.print("\r\n"); // newline
i++;
if(endValue != 0) {
if (endValue != sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2))){
if ((endValue > 268 && endValue < 295 ) || (endValue > 200 && endValue < 225 )){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin6, LOW);
}else{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, HIGH);
}}
}
}
// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}