Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int pinfov = 10;
- const int pinrev = 11;
- const int buttadd = 12;
- const int buttdec = 13;
- int runtime = 1000;
- int runperc = 30;
- int x;
- static unsigned long prev_time, time, lastbutt, period = 0;
- bool dir = true;
- bool run = false;
- void setup()
- {
- Serial.begin(9600);
- pinMode(pinfov, OUTPUT);
- pinMode(pinrev, OUTPUT);
- pinMode(buttadd, INPUT);
- pinMode(buttdec, INPUT);
- time = micros()/1000;
- prev_time = time;
- digitalWrite(pinrev, LOW);
- digitalWrite(pinfov, HIGH);
- x = runtime/200*runperc;
- while (period < x) {
- time = micros();
- period = time - prev_time;
- }
- digitalWrite(pinfov, LOW);
- digitalWrite(pinrev, LOW);
- time = micros()/1000;
- prev_time = time;
- }
- void loop()
- {
- time = micros()/1000;
- period = abs(time - prev_time);
- if (abs(time-lastbutt)>100) {
- lastbutt=time;
- if (digitalRead(buttadd)==HIGH and runperc<100) {
- runperc += 10;
- Serial.println("+ PRESSED");
- }
- if (digitalRead(buttdec)==HIGH and runperc>10) {
- runperc -= 10;
- Serial.println("- PRESSED");
- }
- }
- if (period>runtime){
- prev_time = time;
- dir = !dir;
- run = false;
- }
- if (period % 10 < 1) {
- x = runtime/100*runperc;
- if (period < x) {
- if (dir and !run) {
- digitalWrite(pinfov, LOW);
- digitalWrite(pinrev, HIGH);
- Serial.print("REV: ");
- Serial.println(x);
- run = true;
- } else if (!run) {
- digitalWrite(pinrev, LOW);
- digitalWrite(pinfov, HIGH);
- Serial.print("FOV: ");
- Serial.println(x);
- run = true;
- }
- } else if (run) {
- digitalWrite(pinfov, LOW);
- digitalWrite(pinrev, LOW);
- Serial.print("STOP: ");
- Serial.println(runtime - x);
- run = false;
- } }
- delay (10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement