Comments Widget

Arduino Code (use Version 17 of the Arduino IDE):


/*Counter */

#include <Servo.h>

#include <LiquidCrystal.h>

 

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);   //lcd(12, 11, 5, 4, 3, 2);    // compared against ladyada tutorial which uses (7, 8, 9, 10, 11, 12)

int switchPin = 7;              // switch is connected to pin 2

int val;                        // variable for reading the pin status

int buttonState;                // variable to hold the button state

int buttonPresses = 0;          // how many times the button has been pressed

long seconds = 0;

long minutes = 0;

int pos=0;

int pulse=0;

int degree=90;

Servo myservo;  // create servo object to control a servo

 

 

void setup() {

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

  Serial.begin(9600);           // Set up serial communication at 9600bps

  buttonState = digitalRead(switchPin);   // read the initial state

  lcd.begin(16, 2);

  myservo.attach(9);

  myservo.write(90);

}

 

 

void loop(){

  val = digitalRead(switchPin);      // read input value and store it in val

 

 if (val != buttonState) {          // the button state has changed!

    if (val == HIGH) {                // check if the button is pressed

      buttonPresses++;               // increment the buttonPresses variable

   

     lcd.clear();  //refreshes the LCD screen each time

    

    

     // set the cursor to column 0, line 1

     // (note: line 1 is the second row, since counting begins with 0):

     lcd.setCursor(0, 1);

     lcd.print("Count: ");

     lcd.print(buttonPresses);

    

     //time counting section

     minutes = millis() / 1000 / 60;

     seconds = millis() / 1000 % 60;  

     lcd.setCursor(0, 0);

     lcd.print(minutes);

     lcd.print(" min ");

     lcd.print(seconds);

     lcd.print(" sec");

    

     //servo section

     delay(500);

     while(pulse<95){      //this while loop slows the servo down

     myservo.write(degree);

     degree++;

     pulse++;

     delay(10);  //larger the delay, slower the servo motion

     }

     myservo.write(90);

     delay(300);    

     }  

  }

  buttonState = val;                 // save the new state in our variable

  degree = 90;

  pulse=0;

}