Sunday, December 8, 2013

Some More Code

We decided to leave the push buttons, since the switches were not able to be pushed by the servo motor. While, the buttons were easier to push than originally expected.  So, this is the original code with the motor added to it.  We may have to remove the LCD screen, since there may not be enough pins.  The new code is posted below with the LCD screen unused, since the servo is taking two of its needed pins:
______________________________________________________________________________


//#include <LiquidCrystal.h>
#include <Servo.h>

//LiquidCrystal lcd(5,4,3,2,1,0);     // Intializing the LCD screen's pin connection

Servo myservo1;                     // Creates servo objects to control each servo
Servo myservo2;

const int ledPin13 = 13;            // Intializing each LED's pin connection
const int ledPin12 = 12;    
const int ledPin11 = 11;  
const int ledPin10 = 10;  

int loopCounter = 0;               // Intializing a loop counter
int Mode = 0;                      // Intializing the mode of each sub-system
int switchPin9 = 9;                // Intializing switch pin values
int switchPin8 = 8;
int switchPin7 = 7;
int switchPin6 = 6;
int buttonState1;                  // Intializing the button's state
int buttonState2;      
int buttonState3;      
int buttonState4;      
int value1;                        // Intializing debounce values
int value2;
int value3;
int value4;
int value5;
int value6;
int value7;
int value8;
int pos1 = 0;                      // Intializeing each servo's position    
int pos2 = 0;                          

void setup() {
 
  buttonState1 = digitalRead(switchPin9);     // Reads the swithch pins
  buttonState2 = digitalRead(switchPin8);  
  buttonState3 = digitalRead(switchPin7);  
  buttonState4 = digitalRead(switchPin6);

                                           
  pinMode(switchPin9, INPUT);                 // Set the switch pins as input
  pinMode(switchPin8, INPUT);                
  pinMode(switchPin7, INPUT);                
  pinMode(switchPin6, INPUT);              
  pinMode(ledPin13, OUTPUT);                  // LEDs are set as output
  pinMode(ledPin12, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin10, OUTPUT);

  myservo1.attach(5);                         // attaches the servo to pin 5
  myservo2.attach(4);                         // attaches the servo to pin 4
 
  //lcd.begin(16,2);
  //lcd.clear();
   
  Serial.begin(9600);
}

void loop(){
 
  value1 = digitalRead(switchPin9);           // Read input value and store it
  delay(10);                                  // 10 milliseconds delay
  value2 = digitalRead(switchPin9);           // Reads the input again to check for bounces
  if (value1 == value2) {                     // Make sure intial readings are consistant
    if (value1 != buttonState1) {             // The button's state changed
      if (value1 == LOW) {                    // Check if button is pressed, then flip mode after use
        if (Mode == 0) {              
            Mode = 1;                    
           } else {                    
               Mode = Mode;                
             }      
      }
    }
  buttonState1 = value1;                      // Saves the new state of the variable
  }
  value3 = digitalRead(switchPin8);           // Read input value and store it
  delay(10);                                  // 10 milliseconds delay
  value4 = digitalRead(switchPin8);           // Reads the input again to check for bounces
  if (value3 == value4) {                     // Make sure intial readings are consistant
    if (value3 != buttonState2) {             // The button's state changed
      if (value3 == LOW) {                    // Check if button is pressed, then flip mode after use
        if (Mode == 1) {              
            Mode = 2;                    
        } else {                    
            Mode = Mode;                
          }
      }
    }
  buttonState2 = value3;                      // Saves the new state in our variable
  }
  value5 = digitalRead(switchPin7);           // Read input value and store it
  delay(10);                                  // 10 milliseconds delay
  value6 = digitalRead(switchPin7);           // Reads the input again to check for bounces
  if (value5 == value6) {                     // Make sure intial readings are consistant
    if (value5 != buttonState3) {             // The button's state changed
      if (value5 == LOW) {                    // Check if button is pressed, then flip mode after use
        if (Mode == 2) {            
            Mode = 3;                    
        } else {                    
            Mode = Mode;                  
          }
      }
    }  
  buttonState3 = value5;                     // Saves the new state of the variable
  }
  value7 = digitalRead(switchPin6);          // Read input value and store it
  delay(10);                                 // 10 milliseconds delay
  value8 = digitalRead(switchPin6);          // Reads the input again to check for bounces
  if (value7 == value8) {                    // Make sure intial readings are consistant
    if (value7 != buttonState4) {            // The button's state changed
      if (value7 == LOW) {                   // Check if button is pressed, then flip mode after use
        if (Mode == 3) {
            loopCounter++;                   // Adds to loop counter when mode is 0
            Mode = 0;              
        } else {                    
            loopCounter++;                   // Adds to loop counter when mode is 1        
            Mode = Mode;                  
          }
      }
    }
  buttonState4 = value7;                     // Saves the new state of the variable
  //Serial.print("Number of Loops :  ");       // Prints phrase to screen
  //Serial.println(loopCounter);               // Prints number of loops to LCD screen  
  }


  // save the current state as the last state for next time through the loop
  // Lights turn on and off based on the mode indicated

  if (Mode == 0) {
    digitalWrite(ledPin13, HIGH);
    digitalWrite(ledPin12, LOW);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin10, LOW);
 
    for(pos1 = 0; pos1 < 25; pos1 += 1)     // goes from 0 degrees to 180 degrees in steps of 1 degree
    {                                      
    myservo1.write(pos1);                     // tell servo to go to position in variable 'pos1'
    delay(15);                               // waits 15 milliseconds for the servo to reach the position
    }
  }

  if (Mode == 1) {
    digitalWrite(ledPin13, LOW);
    digitalWrite(ledPin12, HIGH);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin10, LOW);
 
    for(pos2 = 0; pos2 < 40; pos2 += 1)     // goes from 0 degrees to 180 degrees in steps of 1 degree
    {                                      
    myservo2.write(pos2);                     // tell servo to go to position in variable 'pos2'
    delay(15);                               // waits 15 milliseconds for the servo to reach the position
    }
  }

  if (Mode == 2) {
    digitalWrite(ledPin13, LOW);
    digitalWrite(ledPin12, HIGH);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin10, LOW);
 
    for(pos1 = 25; pos1 >= 1; pos1 -= 1)    // goes from 180 degrees to 0 degrees in steps of 1 degree
    {                              
    myservo1.write(pos1);                     // tell servo to go to position in variable 'pos1'
    delay(15);                               // waits 15 milliseconds for the servo to reach the position
    }
  }

  if (Mode == 3) {
    digitalWrite(ledPin13, LOW);
    digitalWrite(ledPin12, LOW);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin10, HIGH);
 
    for(pos2 = 40; pos2 >= 1; pos2 -= 1)    // goes from 180 degrees to 0 degrees in steps of 1 degree
    {                              
    myservo2.write(pos2);                     // tell servo to go to position in variable 'pos2'
    delay(15);                               // waits 15 milliseconds for the servo to reach the position
    }
 
  }

}

No comments:

Post a Comment