#include <Adafruit_GFX.h> // Core graphics library
#include <SPI.h> // this is needed for display
#include <Adafruit_ILI9341.h>
#include <Wire.h> // this is needed for FT6206
#include <Adafruit_FT6206.h>
#define BUZZER 5
// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Size of the color selection boxes and the paintbrush size
#define BOXSIZE 40
#define PENRADIUS 3
#define PUMP 6
#define IR_OBS 4
#define MOTORPIN 3
#define LONG_X 230
#define LONG_Y 170
#define RADIUS 40
#define SHORT_X 90
#define SHORT_Y 170
#define SHORT_BUTTON 1
#define LONG_BUTTON 2
#define NO_BUTTON 0
#define SHORT_TIME 3000
#define LONG_TIME 6000
#define SHAKE_TIME 5000
int oldcolor, currentcolor;
void setup() {
Serial.begin(115200);
Serial.println(F("Cap Touch Paint!"));
init_lcd();
init_buzzer();
init_pump();
init_ir_obs();
init_motor();
screen();
lcd_message("choose your coffee");
}
void loop() {
//test_all();
main_logic();
}
//mainlogic
void main_logic()
{
int button = get_button();
if (button != 0 && cup_present())
{
Serial.println(button);
lcd_message("pouring coffee");
pour_coffee (button);
lcd_message ("shaking cup");
shake_cup ();
lcd_message ("coffee is ready");
sound_buzzer ();
delay (2000);
lcd_message("choose your coffee");
}
}
//init
void init_motor()
{
pinMode(MOTORPIN, OUTPUT);
}
void init_pump()
{
pinMode(PUMP, OUTPUT);
}
void init_buzzer()
{
pinMode(BUZZER, OUTPUT);
}
void init_lcd()
{
tft.begin();
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen started");
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
}
void init_ir_obs()
{
pinMode(IR_OBS, INPUT);
}
//test
void test_all()
{
// test_lcd();
test_buzzer();
test_pump();
test_ir_obs();
test_motor();
}
void test_lcd()
{
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
if (! ctp.touched()) {
return;
}
TS_Point p = ctp.getPoint();
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
}
void test_buzzer()
{
tone (BUZZER, 1000);//Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(BUZZER); // Stop sound...
delay(1000); // ...for 1sec
}
void test_pump()
{
digitalWrite(PUMP, HIGH);
delay(1000);
digitalWrite(PUMP, LOW);
delay(1000);
}
void test_ir_obs()
{
Serial.print("IR obstacle: ");
Serial.println(digitalRead(IR_OBS));
delay(1000);
}
void test_motor()
{
digitalWrite(MOTORPIN, HIGH);
delay(1000);
digitalWrite(MOTORPIN, LOW);
delay(1000);
}
//lcd
void screen()
{
tft.drawRect(50, 20, 220, 80, ILI9341_WHITE);
tft.drawCircle(SHORT_X, SHORT_Y, RADIUS, ILI9341_WHITE);
tft.setCursor(60, 160);
tft.print("short");
tft.drawCircle(LONG_X, LONG_Y, RADIUS, ILI9341_WHITE);
tft.setCursor(205, 160);
tft.print("long");
}
void lcd_message(String message)
{
tft.fillRect(51, 21, 218, 78, ILI9341_BLACK);
tft.setCursor(53, 45);
tft.print(message);
}
int get_button()
{
int x, y;
if (! ctp.touched()) {
return NO_BUTTON;
}
TS_Point p = ctp.getPoint();
x = map(p.y, 0, 320, 0, 320);
y = map(p.x, 0, 240, 240, 0);
if (x > (SHORT_X - RADIUS) && x < (SHORT_X + RADIUS) && y > (SHORT_Y - RADIUS) && y < (SHORT_Y + RADIUS) )
return SHORT_BUTTON;
else if (x > (LONG_X - RADIUS) && x < (LONG_X + RADIUS) && y > (LONG_Y - RADIUS) && y < (LONG_Y + RADIUS) )
return LONG_BUTTON;
else
return NO_BUTTON;
}
//pump
void pour_coffee(int button)
{
if (button == SHORT_BUTTON)
open_pump (SHORT_TIME);
else if (button == LONG_BUTTON)
open_pump(LONG_TIME);
close_pump();
}
void open_pump(int duration)
{
digitalWrite(PUMP, HIGH);
delay(duration);
}
void close_pump()
{
digitalWrite (PUMP, LOW);
}
//irobs
bool cup_present()
{
bool cup_present = digitalRead(IR_OBS);
if (cup_present)
Serial.println("cup is present");
return cup_present;
}
//motor
void shake_cup()
{
digitalWrite(MOTORPIN, HIGH);
delay(SHAKE_TIME);
digitalWrite(MOTORPIN, LOW);
}
//buzzer
void sound_buzzer()
{
tone (BUZZER, 1000);//Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(BUZZER); // Stop sound...
}