Skip to content

Instantly share code, notes, and snippets.

@tomastitera
Last active June 17, 2018 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomastitera/a73261259a7c1933ad6cefbfd90252c3 to your computer and use it in GitHub Desktop.
Save tomastitera/a73261259a7c1933ad6cefbfd90252c3 to your computer and use it in GitHub Desktop.
Naše země nevzkvétá
//Project uses the following Arduino libraries: TaskScheduler, TinkerKit, Wire, LiquidCrystal_I2C and Servo.
#include <TaskScheduler.h>
#include <TinkerKit.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
//Set the LCD address to 0x27 for a 16 chars and 2 line display.
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Initialize TinkerKit Button as Input 0
TKButton button(I0);
//Initialize TinkerKit Continuous rotation servo
Servo TKContinuousRotationServo;
//Initialize TinkerKit LED as Output 1
TKLed led(O1);
//Initialize 4 callback actions for the task scheduler
void t1Callback();
void t2Callback();
void t3Callback();
void t4Callback();
//Initializie runner with all the task scheduled. The tasks run in a loop.
//Initialize tasks responding to the callback actions. The number in brackets indicates how many times per second the task is performed.
Task tbutton(50, TASK_FOREVER, &t1Callback);
Task tchapadlo(115, TASK_FOREVER, &t2Callback);
Task tled(3000, TASK_FOREVER, &t3Callback);
Task tdisplay(1000, TASK_FOREVER, &t4Callback);
Scheduler runner;
//Task for reading button's state. If the button is on, the servo with the tentacle, the LED and the LCD are enabled.
//When on, the LCD displays Havel's motto ("Truth and love must prevail over lies and hatred").
//If the button is off, the periferies are disabled.
void t1Callback() {
int val = button.readSwitch();
if (val) {
tchapadlo.enableIfNot();
tled.enableIfNot();
tdisplay.enableIfNot();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" Pravda a laska");
lcd.setCursor(0, 1);
lcd.print(" musi zvitezit");
} else {
tchapadlo.disable();
tled.disable();
tdisplay.disable();
lcd.noBacklight();
lcd.clear();
}
}
//Task for counting the revolutions of the servo.
//If the count is even (division by two leaves no remainder), servo is set to position 5. If the count is odd, it is set to position 175
void t2Callback() {
int val = tchapadlo.getRunCounter() % 2 ? 5 : 175;
TKContinuousRotationServo.write(val);
}
//Task for turning the LED on/off
void t3Callback() {
int val = tled.getRunCounter() % 2;
Serial.println( val);
if (val) {
led.on();
} else {
led.off();
}
}
//Task for stopping the servo and switching the led off.
void t4Callback() {
}
void chapadloDisable() {
TKContinuousRotationServo.write(90);
}
void ledDisable() {
led.off();
}
//Runner setup - includes all the tasks above.
void setup () {
Serial.begin(115200);
TKContinuousRotationServo.attach(O0);
runner.init();
runner.addTask(tbutton);
runner.addTask(tchapadlo);
runner.addTask(tled);
tchapadlo.setOnDisable(&chapadloDisable);
tled.setOnDisable(&ledDisable);
tbutton.enable();
lcd.begin();
}
void loop () {
runner.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment