Sketch optimization?
hi everyone...i modified existing sketch can input passcode pressing 3 buttons, each button allows scroll through numbers 1-9. after setting correct code (2-4-1), led changes/flashes. there lcd attached prompts , small servo heck of it, moves position based on whether code correct or not.
the sketch simple , works wanted do. however, being newbie, wondering if had advice on optimization/simplification future projects.
would grateful suggestions.
thx!
the sketch simple , works wanted do. however, being newbie, wondering if had advice on optimization/simplification future projects.
would grateful suggestions.
thx!
so don't have download file:
code: [select]
#include <servo.h>
#include <liquidcrystal.h>
liquidcrystal lcd(11, 10, 6, 7, 8, 9);
servo myservo;
const int buttonpin1 = 2; //buttons 1, 2 , 3
const int buttonpin2 = 4;
const int buttonpin3 = 5;
const int ledpingreen = 12;
const int ledpinred = 13;
// variables change:
int buttonpushcounter1 = 0;
int buttonpushcounter2 = 0;
int buttonpushcounter3 = 0;
int buttonstate1 = 0;
int buttonstate2 = 0;
int buttonstate3 = 0;
int lastbuttonstate1 = 0;
int lastbuttonstate2 = 0;
int lastbuttonstate3 = 0;
void setup() {
myservo.attach(3);
lcd.begin(16,2);
lcd.print("enter the");
lcd.setcursor(0,1);
lcd.print("code!");
pinmode(buttonpin1, input);
pinmode(buttonpin2, input);
pinmode(buttonpin3, input);
pinmode(ledpingreen, output);
pinmode(ledpinred, output);
digitalwrite(ledpinred, high); //a little led flashing
digitalwrite(ledpingreen, high);
delay(100);
digitalwrite(ledpinred, low);
digitalwrite(ledpingreen, low);
delay(100);
serial.begin(9600);
digitalwrite (ledpinred, high);
digitalwrite (ledpingreen, low);
myservo.write(0);
}
void loop() {
buttonstate1 = digitalread(buttonpin1); //button 1
if (buttonstate1 != lastbuttonstate1) {
if (buttonstate1 == high) {
buttonpushcounter1++;
serial.println("on");
serial.print("number of button1 pushes: ");
serial.println(buttonpushcounter1);
} else {
serial.println("off");
}
delay(50);
}
lastbuttonstate1 = buttonstate1;
buttonstate2 = digitalread(buttonpin2); //button 2
if (buttonstate2 != lastbuttonstate2) {
if (buttonstate2 == high) {
buttonpushcounter2++;
serial.println("on");
serial.print("number of button2 pushes: ");
serial.println(buttonpushcounter2);
} else {
serial.println("off");
}
delay(50);
}
lastbuttonstate2 = buttonstate2;
buttonstate3 = digitalread(buttonpin3); //button 3
if (buttonstate3 != lastbuttonstate3) {
if (buttonstate3 == high) {
buttonpushcounter3++;
serial.println("on");
serial.print("number of button3 pushes: ");
serial.println(buttonpushcounter3);
} else {
serial.println("off");
}
delay(50);
}
lastbuttonstate3 = buttonstate3;
//lcd display while entering code
if (buttonstate1 >= 1 || buttonstate2 >=1 || buttonstate3 >=1) {
lcd.clear();
lcd.print("entering...");
lcd.setcursor(0,1);
lcd.print(buttonpushcounter1);
lcd.print("-");
lcd.print(buttonpushcounter2);
lcd.print("-");
lcd.print(buttonpushcounter3);
}
//once each button pushed 9 times, starts again @ 0 can scroll through set of numbers
if (buttonpushcounter1 == 10) {
buttonpushcounter1 = 0;
}
if (buttonpushcounter2 == 10) {
buttonpushcounter2 = 0;
}
if (buttonpushcounter3 == 10) {
buttonpushcounter3 = 0;
}
if (buttonpushcounter1 == 2) //the correct code
if (buttonpushcounter2 == 4)
if (buttonpushcounter3 == 1) {
delay(50);
digitalwrite(ledpingreen, high);
digitalwrite(ledpinred, low);
delay(50);
lcd.clear();
lcd.print("you did it!");
myservo.write(50); //servo moves/points :)
} else {
digitalwrite(ledpingreen, low);
digitalwrite(ledpinred, high);
}
//if wanted limit input number of attempts
if (buttonpushcounter1 + buttonpushcounter2 + buttonpushcounter3 == 1000) {
digitalwrite(ledpinred, low);
delay(500);
digitalwrite(ledpinred, high);
delay(500);
digitalwrite(ledpinred, low);
delay(500);
digitalwrite(ledpinred, high);
lcd.clear();
lcd.print("wrong!"); //code incorrect after number of attemps
delay(1000);
lcd.clear();
lcd.print("try again!");
buttonpushcounter1 = 0;
buttonpushcounter2 = 0;
buttonpushcounter3 = 0;
myservo.write(2000); //servo moves/points :( moves initial position
delay(1000);
myservo.write(0);
}
}
Arduino Forum > Using Arduino > Programming Questions > Sketch optimization?
arduino
Comments
Post a Comment