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);
}
}
}