push button controlling different modes of servo not working

Below you will see the different codes. The first one is a pushbutton moving throug different LED modes . The next one is of memsic 2125 controlling a servo. The third one is a combination of both.

I know the memsic controlling the servo works and the bushbutton controlling the LEDs works.

So the problem is when I combined them both I cant get the servo to work. The leds do what they are supposed to but the servos dont do anything. The reason I left the LED in the different modes is make sure the pushbutton actually is cycling through when I press it. I dont really care about the LEDs. Its the servos thats been stumping me.

Can someone please let me know what piece of code im missing to make the servos follow the memsic?

Push button debounce controlling different led modes

int switchPin = 2;              // switch is connected to pin 2
int led1Pin = 12;


int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int lightMode = 0;              // What mode is the light in?

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  pinMode(led1Pin, OUTPUT);

  
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(20);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn lights on!
        } else {
          if (lightMode == 1) {        // if its all-on
            lightMode = 2;             // make it blink!
          } else {
            if (lightMode == 2) {      // if its blinking
              lightMode = 3;           // make it wave!
            } else {
			  if (lightMode == 3) { //  if its waving, 
                lightMode = 0;           // turn light off!
              }
			}
          }
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
  if (lightMode == 0) { // all-off
    digitalWrite(led1Pin, LOW);

  }

  if (lightMode == 1) { // all-on
    digitalWrite(led1Pin, HIGH);
 
  }

  if (lightMode == 2) { // blinking
    digitalWrite(led1Pin, HIGH);
   
    delay(100);
    digitalWrite(led1Pin, LOW);
  
    delay(100);
  }
  if (lightMode == 3)  { // "wave"
 
    digitalWrite(led1Pin, HIGH);
    delay(50);
    digitalWrite(led1Pin, LOW);
 
  }    
}

Memsic 2125 controlling servo on x axis

#include <Servo.h> 

const int xPin = 2;		 // X output of the accelerometer

const int yPin = 3;		 // Y output of the accelerometer
 
Servo myservo;


int val;

void setup() {

  // initialize serial communications:

  Serial.begin(9600);

  // initialize the pins connected to the accelerometer
 
  // as inputs:

  pinMode(xPin, INPUT);

  pinMode(yPin, INPUT);

  myservo.attach(9);

}




void loop() {

  // variables to read the pulse widths:
 
  int pulseX, pulseY;

  // variables to contain the resulting accelerations

  int accelerationX, accelerationY;

  

  // read pulse from x- and y-axes:

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

  




  accelerationX = ((pulseX / 10) - 500) * 8;

  accelerationY = ((pulseY / 10) - 500) * 8;



 val = (accelerationX);
val = map(val, 0,1000,0,90);


if (accelerationX >0)
{
  myservo.write(val);
}
else
{
  myservo.write(-val);
}


  // print the acceleration

  Serial.print(accelerationX);

  // print a tab character:

  Serial.print("\t");
 
  Serial.print(accelerationY);

  Serial.println();




  delay(100);

}

Combination of both

#include <Servo.h> 
int switchPin = 4;              // switch is connected to pin 4
int led1Pin = 13;
const int xPin = 2;		 // X output of the accelerometer

const int yPin = 3;
Servo myservo;

int servoMode = 0;
int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int val3;                  // variable for reading memsic
int buttonState;                // variable to hold the button state

int lightMode = 0;              // What mode is the light in?

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  pinMode(led1Pin, OUTPUT);

   pinMode(xPin, INPUT);

  pinMode(yPin, INPUT);

  myservo.attach(9);
  
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
      // variables to read the pulse widths:
      
    int pulseX, pulseY;

  // variables to contain the resulting accelerations

  int accelerationX, accelerationY;

  

  // read pulse from x- and y-axes:

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

// convert the pulse width into acceleration
  // accelerationX and accelerationY are in milli-g's: 
  // earth's gravity is 1000 milli-g's, or 1g.
  accelerationX = ((pulseX / 10) - 500) * 8;

  accelerationY = ((pulseY / 10) - 500) * 8;



 val3 = (accelerationX);
val3 = map(val, 0,1000,0,90);

 Serial.print(accelerationX);

  // print a tab character:

  Serial.print("\t");
 
  Serial.print(accelerationY);

  Serial.println();
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(20);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn lights on!
        } else {
          if (lightMode == 1) {        // if its all-on
            lightMode = 2;             // make it blink!
          } else {
            if (lightMode == 2) {      // if its blinking
              lightMode = 3;           // make it wave!
            } else {
	      if (lightMode == 3) { //  if its waving, 
                lightMode = 0;           // turn light off!
              }
			}
          }
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
  if (lightMode == 0) { 
    if (accelerationX >0){
       myservo.write(val3);
    digitalWrite(led1Pin, LOW);

  }
  else
{
  myservo.write(-val3);
  }
  }
  if (lightMode == 1) { 
  if (accelerationX >0)
{
  myservo.write(-val3);
    digitalWrite(led1Pin, HIGH);
}else
{
  myservo.write(+val3);
  }
  }

  if (lightMode == 2) { 
  if (accelerationX >0)
{
  myservo.write(val3/3);
    digitalWrite(led1Pin, HIGH);
   
    delay(100);
    digitalWrite(led1Pin, LOW);
  
    delay(100);
  }else
{
  myservo.write(-val3/3);
 
  } }
  if (lightMode == 3)  { 
     if (accelerationX >0)
{
  myservo.write(-val3/3);
 
    digitalWrite(led1Pin, HIGH);
    delay(50);
    digitalWrite(led1Pin, LOW);
}else
{
  myservo.write(val3/3);
 
  } 
  }    
}

The random indenting and excess white space and extra parentheses where not needed make your code hard to read. I was trying, anyway, until I got to this:

  if (lightMode == 0) { 
    if (accelerationX >0){
       myservo.write(val3);
    digitalWrite(led1Pin, LOW);

  }
  else
{
  myservo.write(-val3);
  }
  }

Put each { and } on separate lines, then use Tools + Auto Format to format your code into a readable version. Repost that code.

        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn lights on!
        } else {
          if (lightMode == 1) {        // if its all-on
            lightMode = 2;             // make it blink!
          } else {
            if (lightMode == 2) {      // if its blinking
              lightMode = 3;           // make it wave!
            } else {
          if (lightMode == 3) { //  if its waving, 
                lightMode = 0;           // turn light off!
              }
            }

This is an awful lot of poorly nested code to do this:

   lightMode++;
   if(lightMode > 3)
     lightMode = 0;

Hey PaulS. Thanks for the advice. I didnt know that I could make it that much shorter. I went ahead and made the changes you suggested. After cleaning everything up I found the reason my servos were not working.

When mapping the accelerometer I wrote val instead of val3

val3 = (accelerationX);
val3 = map(val,0,1000,0,90);

When mapping the accelerometer I wrote val instead of val3

Copying and pasting will bite you every time. When you feel the urge to copy and paste, consider cutting and pasting, instead. Create a function that you move the code to, and call that function, instead.

By the way, the parentheses on that first line are not needed, and, in my humble opinion, look silly.

. Create a function that you move the code to, and call that function, instead.

Can you give me an example of how to do this?

Also which parenthesis are you talking about?

Thanks for the advice.

roybenmo: Can you give me an example of how to do this?

datatype func_name(input_datatype inputdata_name){
    //Your Functions
}

Here is Arduino's examples http://www.arduino.cc/en/Reference/FunctionDeclaration

And here's one of mine from a recent project

void setLight(int red, int green, int blue, int lightnum){
  Tlc.set(0+(3*lightnum), red);
  Tlc.set(1+(3*lightnum), green);
  Tlc.set(2+(3*lightnum), blue);
  Tlc.update();
}

So where in my code would I apply it to?

There's really no payoff in this particular instance, although of course it is good practice in general. You would do it if you had multiple similar sections of code. In your case though, about the most useful change you could make there is to change this:

val3 = (accelerationX); // These are the unnecessary parenthesis PaulS refers to 
val3 = map(val3,0,1000,0,90);

to this:

val3 = map(accelerationX,0,1000,0,90);