Frustrated @ Accelerometer and MP3 Code!

:astonished:
I need help. I know this is user error, I’ve checked all the hardware multiple times. I’m not exactly new to programming, but I can’t, for the life of me, figure out why this code doesn’t work like I think it should…
Basically, when the accelerometer is moved past either 500 or -500 on the X or Y axis, it should trigger nextPin. nextPin is a “Next” button on an MP3 player, I want the MP3 to change ANY time this happens. Instead, when I run this code, it seems like my Arduino waits for one second, then begins to cycle through the MP3s, with no regard to the position of the accelerometer. The “Serial.begin” is only in there for my own testing, to see if for some reason the accelerometer wasn’t responding, something wasn’t working, etc… I don’t know if I need to tell the Arduino to set nextPin as LOW after setting it as HIGH, to avoid keeping 5v on that pin and cycling through the songs. I have no idea, I hope someone can help me…

const int powerPin = 2, nextPin = 4, prevPin = 3, xPin = 5, yPin = 6;

void setup() {                
 
  pinMode((powerPin, nextPin, prevPin), OUTPUT);
  pinMode((xPin, yPin), INPUT);
  Serial.begin(9600);
}

void loop() {
  int pulseX, pulseY, accelX, accelY;
  
  pulseX = pulseIn(xPin,HIGH);  
  pulseY = pulseIn(yPin,HIGH);
  
  accelX = ((pulseX / 10) - 500) * 8;
  accelY = ((pulseY / 10) - 500) * 8;
  
  digitalWrite(nextPin, LOW);
  Serial.print(accelX);
  Serial.print("\t");
  Serial.print(accelY);
  Serial.println();
  delay(1000);
      //start if loop
      
      if(((accelX) || (accelY) > 500) || ((accelX) || (accelY) < -500))
          digitalWrite(nextPin, HIGH);
          delay(10);
          digitalWrite(nextPin, LOW);
      
}
      //end if loop

code problems... nah.jpg

If you indent this properly with the auto-indent tool in the IDE you get this:

const int powerPin = 2, nextPin = 4, prevPin = 3, xPin = 5, yPin = 6;

void setup() {                

  pinMode((powerPin, nextPin, prevPin), OUTPUT);
  pinMode((xPin, yPin), INPUT);
  Serial.begin(9600);
}

void loop() {
  int pulseX, pulseY, accelX, accelY;

  pulseX = pulseIn(xPin,HIGH);  
  pulseY = pulseIn(yPin,HIGH);

  accelX = ((pulseX / 10) - 500) * 8;
  accelY = ((pulseY / 10) - 500) * 8;

  digitalWrite(nextPin, LOW);
  Serial.print(accelX);
  Serial.print("\t");
  Serial.print(accelY);
  Serial.println();
  delay(1000);
  //start if loop

  if(((accelX) || (accelY) > 500) || ((accelX) || (accelY) < -500))
    digitalWrite(nextPin, HIGH);
  delay(10);
  digitalWrite(nextPin, LOW);

}
//end if loop

First, it’s a tiny bit weird having the delay(1000) there. Do you want it to be slow to respond?

Second, as you can see, only one thing is done by the if line. The other two things are done unconditionally. You need to read up on using braces to make blocks of code.

In other words:

  if(((accelX) || (accelY) > 500) || ((accelX) || (accelY) < -500))
    {
    digitalWrite(nextPin, HIGH);
    delay(10);
    digitalWrite(nextPin, LOW);
    }

This code started out a lot simpler, I have tweaked it a bunch since it originally was. I did have brackets around the “if” statement first, the problem still occurred though, so I took them out to see if maybe that had something to do with it. The delay also was put in after, so that the Serial.begin command wasn’t giving me tons of data. Let me post the original code, maybe that will help.

const int powerPin = 2, nextPin = 4, prevPin = 3, xPin = 5, yPin = 6;

void setup() {                
 
  pinMode((powerPin, nextPin, prevPin), OUTPUT);
  pinMode((xPin, yPin), INPUT);
}

void loop() {
  int pulseX, pulseY, accelX, accelY;
  
  pulseX = pulseIn(xPin,HIGH);  
  pulseY = pulseIn(yPin,HIGH);
  
  accelX = ((pulseX / 10) - 500) * 8;
  accelY = ((pulseY / 10) - 500) * 8;
  
  digitalWrite(nextPin, LOW);

      //start if loop
      
      if((accelX || accelY > 500) || (accelX || accelY < -500)){
          digitalWrite(nextPin, HIGH);
          delay(10);
          digitalWrite(nextPin, LOW);
      }
      
}
      //end if loop
if((accelX || accelY > 500) || (accelX || accelY < -500)){

Explain, in your own words, what you think this is doing.

Something like, if the measurement of accelX or accelY is greater than 500 or less than -500, then...

Something like, if the measurement of accelX or accelY is greater than 500 or less than -500, then…

Close, but no cigar.

The test does not work like that. If you want to compare two values, you must compare them one at a time:

if(accelX > 500 || accelY > 500 || accelX < -500 || accelY < -500)

Ahhhhhhh, alright... This whole time I've been trying to make it as simple as possible, never thinking that the syntax I used was wrong... Let me give it a whirl.

Even using that, the Arduino waits about a second and then begins to cycle through each song. I measured the voltage coming through nextPin, it's a solid 3ish volts... Which tells me that pin is HIGH. And I don't know why...

  pinMode((powerPin, nextPin, prevPin), OUTPUT);
  pinMode((xPin, yPin), INPUT);

You are not allowed to manufacture shortcuts...