Hi there,
I am trying my first project for an automated cat feeder, I have some older code which works perfectly now I need to integrate it with thinger.io and I think I need some parallel proccesing I think to create a nice online dashboard.
I have a servo, a LCD and a Real time Clock. In the current code the servo responds when I press a button and the clock displays fine on my LCD. I can’t use delays in my thinger code. So my question is how can I write the code to have the LCD and clock work in Thinger? And integrate the LCD within thinger? I need parallel processing right? I cant use a delay in my main code i mean?
My current code I want to migrate to thinger.io
#include <virtuabotixRTC.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <Servo.h>
#include <SPI.h>
#define I2C_ADDR 0x3F // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
Servo servo1;
int n = 1;
const int buttonPin = 2; // analog pin used to connect the button
int buttonState = 0;
int pos = 0; // variable to store the servo position
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
// Creation of the Real Time Clock Object
virtuabotixRTC myRTC(5, 6, 7);
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
//servo setup
servo1.attach(A0); //analog pin 0
servo1.write(1); //set servo to position 0
// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
//myRTC.setDS1302Time(00, 14, 18, 5, 13, 11, 2016);
//set time for alarm and set alarm
// setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
//setTime(myRTC.hours,myRTC.minutes,0,myRTC.dayofmonth,myRTC.month,16); // set time to Saturday 8:29:00am Jan 1 2011
myRTC.updateTime(); //|
setTime(myRTC.hours,myRTC.minutes,myRTC.seconds,0,0,0); // set time to Saturday 8:29:00am Jan 1 2011
Alarm.alarmRepeat(17,5,0, MorningAlarm); // 8:30am every day
digitalClockDisplay();
//Alarm.delay(1000); // wait one second between clock display NOT VERY GOOD FOR THINGER.IO
lcd.begin (16,2); // <<----- My LCD was 16x2
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print(“Pieter’s FoodSyS”);
}
//=======================================================================================================//|
// //|
// Printout by accessing Single Element objects BEGIN //|
// //|
//=======================================================================================================//|
// //|
// This example utilizes the Serial.print function to access individual data elements, this allows for //|
// user defined output format. //|
// //|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//|
void loop() { //|
//button control
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
lcd.home (); // go home
lcd.clear();
lcd.print(“Serv.Food Manual”);
servo1.write(179);
}
else {
// turn LED off:
servo1.write(1);
lcd.home (); // go home
lcd.clear();
lcd.print(“Pieter’s FoodSyS”);
}
// This allows for the update of variables for time or accessing the individual elements. //|
myRTC.updateTime(); //|lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(myRTC.hours);
lcd.print(“:”);
lcd.print(myRTC.minutes);
lcd.print(“:”);
lcd.print(myRTC.seconds);
lcd.setBacklight(HIGH); // Backlight on//digitalClockDisplay();
//|
// Start printing elements as individuals //|
//Serial.print(“Current Date / Time: “); //|
// Serial.print(myRTC.dayofmonth); //|
// Serial.print(”/”); //|
// Serial.print(myRTC.month); //|
// Serial.print(“/”); //|
// Serial.print(myRTC.year); //|
// Serial.print(" “); //|
// Serial.print(myRTC.hours); //|
// Serial.print(”:“); //|
// Serial.print(myRTC.minutes); //|
// Serial.print(”:"); //|
// Serial.println(myRTC.seconds); //|
//|
// Delay so the program doesn’t print non-stop //|
Alarm.delay(1000); //|
} //|
//|
//=======================================================================================================//|
// //|
// Printout using BUFFER objects BEGIN //|
// //|
//=======================================================================================================//|
void MorningAlarm(){
Serial.println(“event triggered”);
Food();
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits)
{
Serial.print(“:”);
if(digits < 10)
Serial.print(‘0’);
Serial.print(digits);
}
void Food() {
lcd.home (); // go home
lcd.clear();
lcd.print(“Serving Food”);
servo1.write(170);
Alarm.delay(3000);
servo1.write(1);
Alarm.delay(1500);
lcd.home (); // go home
lcd.print(“Pieter’s FoodSyS”);
}
I need to integrate it with the thinger framework code somehow:
#include <Adafruit_CC3000.h> #include <SPI.h> #include <ccspi.h> #include <ThingerCC3000.h> #include <Servo.h>
#define USERNAME "your_username"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"
#define SSID "your_ssid"
#define SSID_PASSWORD "your_ssid_password"
#define SERVO_PIN 9
ThingerCC3000 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
Servo servo;
void setup() {
servo.attach(SERVO_PIN);
// configure wifi network
thing.add_wifi(SSID, SSID_PASSWORD);
// controlling servo position
thing["servo"]["pos"] << [](pson& in){
servo.write((int)in);
};
// reseting servo position
thing["servo"]["reset"] = [](){
servo.write(0);
};
}
void loop() {
thing.handle();
}
What would be the best approach to have my LCD and clock working inside thinger code? (running independently I think)? My LCD code seems to break the thinger.io code.
Many thanks for any advice! I will make a tutorial of this when I get it to work with pictures!