Pages: [1]   Go Down
Author Topic: Accelerometer & push button  (Read 440 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
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;
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey,
     For that connect your button on Interrupt and in his ISR make flag = 1
 after that
 where your code turn on led( it basically a condition which you are using to turn on LED) so just use or(||) condition to turn on led or off. 

So now what happened for turning LED your are using two condition if any of them is true then Led must glow. that it

Is it make sens.....???
 
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code layout isn't brilliant - you'll find the control structures much easier to see and understand if you put each { and } on separate lines with matching pairs indented by the same amount and the bits between them indented one extra level.

The button processing logic looks OK (although you seem to be confused about the difference between speakers and LEDs assuming you have a momentary button connected and want to toggle the output state each time you press the button. However, the accelerometer processing seems to simply turn the outputs on and off according to the amount of acceleration - it doesn't seem to be toggling them on and off as the switch code does. Hence, whatever your switch logic might do will immediately be overwritten by the outputs set from the accelerometer code. I think you need to decide how these two sets of inputs will be combined to control the LEDs. In the short term you could test whether the button logic actually does what you expect by disabling the code handling acceleration.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: