#include <TM1637Display.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Wire.h>
#define SERVO_PIN 7
#define CLK 9
#define DIO 10
#define IR_SENSOR 2
#define POTENTIOMETER A0
#define SW_PIN 4
#define STIR_TIME 5000
//initialize the liquid crystal library
//the first parameter is the I2C address
//the second parameter is how many rows are on your screen
//the third parameter is how many columns are on your screen
LiquidCrystal_I2C lcd(0x27, 16, 2);
TM1637Display display = TM1637Display(CLK, DIO);
Servo myservo;
const int maxspeed = 700;
int speed;
const int stop = 1500;
void setup() {
Serial.begin(9600);
init_lcd();
init_speed();
init_display();
init_ir();
init_servo();
init_switch();
}
void loop() {
//test_all();
main_logic();
}
void main_logic()
{
speed=get_speed();
if (is_cup())
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("speed: ");
lcd.print(speed);
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("place your cup");
}
if (button_pushed() && is_cup())
{
stir_cup(get_speed(),STIR_TIME);
lcd.clear();
lcd.print("take your cup");
}
}
// main logic
bool is_cup()
{
return digitalRead(IR_SENSOR);
}
bool button_pushed()
{
return digitalRead(SW_PIN);
}
int get_speed()
{
int val=analogRead(POTENTIOMETER);
val=map(val,0,1023,0,255);
return val;
}
void stir_cup(int speed,int wait)
{
rotate( speed );
int counter=wait/1000;
for (int i=counter;i>0;i--)
{
display.showNumberDec(i);
delay(1000);
display.clear();
}
rotate(0);
}
// init
void init_lcd()
{
//initialize lcd screen
lcd.init();
// turn on the backlight
lcd.backlight();
}
void init_speed()
{
pinMode(POTENTIOMETER, INPUT);
}
void init_display()
{
display.clear();
display.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest)
}
void init_ir()
{
pinMode(IR_SENSOR, INPUT);
}
void init_servo()
{
myservo.attach(SERVO_PIN);
}
void init_switch()
{
pinMode(SW_PIN, INPUT);
}
//test
void test_all()
{
//test_lcd();
//test_speed();
//test_display();
//test_ir();
//test_servo();
test_switch();
}
void test_lcd()
{
//wait for a second
delay(1000);
// tell the screen to write on the top row
lcd.setCursor(0,0);
// tell the screen to write “hello, from” on the top row
lcd.print("eee");
// tell the screen to write on the bottom row
lcd.setCursor(0,1);
// tell the screen to write “Arduino_uno_guy” on the bottom row
// you can change whats in the quotes to be what you want it to be!
lcd.print("eee");
}
void test_speed()
{
int val=analogRead(POTENTIOMETER);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(val);
}
void test_display()
{
int i;
for (i = 0; i < 10; i++) {
display.showNumberDec(i);
delay(500);
display.clear();
}
}
void test_ir()
{
Serial.print("IR obstacle: ");
Serial.println(digitalRead(IR_SENSOR));
delay(500);
}
void test_servo()
{
Serial.println("Stop");
rotate(0);
delay(5000);
Serial.println("minus");
rotate(-100);
delay(5000);
Serial.println("Stop");
rotate(0);
delay(5000);
Serial.println("plus");
rotate(+100);
delay(5000);
}
void test_switch()
{
bool Switch_Pushed = digitalRead(SW_PIN);
Serial.print("switch is: ");
Serial.println(Switch_Pushed);
}
//servo
void rotate(int speed) {
speed = map(speed, -100, +100, stop - maxspeed, stop + maxspeed);
myservo.writeMicroseconds(speed);
}