-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_arduino
173 lines (120 loc) · 3.13 KB
/
code_arduino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <Wire.h>
#include <Servo.h>
#include <string.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
#define IN1 22
#define IN2 24
#define MAX_SPEED 255 //từ 0-255
#define MIN_SPEED 0
Servo servo_day;
Servo servo_1;
Servo servo_2;
int dem1, dem2, dem3;
void motor_Dung() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
void motor_Tien(int speed) { //speed: từ 0 - MAX_SPEED
speed = constrain(speed, MIN_SPEED, MAX_SPEED);//đảm báo giá trị nằm trong một khoảng từ 0 - MAX_SPEED - http://arduino.vn/reference/constrain
digitalWrite(IN1, HIGH);// chân này không có PWM
analogWrite(IN2, 255 - speed);
}
void motor_Lui(int speed) {
speed = constrain(speed, MIN_SPEED, MAX_SPEED);//đảm báo giá trị nằm trong một khoảng từ 0 - MAX_SPEED - http://arduino.vn/reference/constrain
digitalWrite(IN1, LOW);// chân này không có PWM
analogWrite(IN2, speed);
}
void setup()
{
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
Serial.begin(9600);
servo_day.attach(32);
servo_1.attach(31);
servo_2.attach(30);
lcd.begin(20, 4); // Initialize LCD
lcd.setCursor(3, 0); // Set the cursor at the 4th column and 1st row
lcd.print("product1");
lcd.setCursor(8, 1); // Set the cursor at the 9th column and 2nd row
lcd.print("produc2");
lcd.setCursor(0, 2); // Set the cursor at the 1st column and 3rd row
lcd.print("product3");
lcd.setCursor(8, 3); // Set the cursor at the 9th column and 4th row
lcd.print("****");
dem1 = 0;
dem2 = 0;
dem3 = 0;
servo_1.write(0);
servo_2.write(0);
for (int i = 80; i >= 0; i--)
{
servo_day.write(i);
delay(50);
}
servo_day.write(80);
delay(3000);
}
void loop()
{
motor_Dung();
int gt = 0;
delay(1000);
Serial.println(gt);
if (Serial.available() > 0)
{
String str = Serial.readString();
Serial.flush();
Serial.println(str);
char cstr[str.length() + 1];
str.toCharArray(cstr, str.length() + 1);
if (strchr(cstr, '1') != NULL)
{
servo_1.write(40);
delay(20);
motor_Tien(255);
delay(5000);
servo_1.write(0);
delay(20);
motor_Dung();
dem1 = dem1 + 1; //tang gia tri sp 1 len 1
lcd.setCursor(6, 0); //hien thi ra lcd
lcd.print(round(dem1));
//servo_day.write(80);
//delay(3000);
}
if (strchr(cstr, '2') != NULL)
{
servo_2.write(40);
delay(20);
motor_Tien(255);
delay(5000);
servo_2.write(0);
delay(20);
motor_Dung();
dem2 = dem2 + 1;
lcd.setCursor(10, 1); //hien thi lcd
lcd.print(round(dem2));
//servo_day.write(80);
//delay(3000);
}
if (strchr(cstr, '3') != NULL)
{
motor_Tien(255);
delay(5000);
motor_Dung();
dem3 = dem3 + 1; //tang gia tri sp 1 len 1
lcd.setCursor(2, 2); //hien thi ra lcd
lcd.print(round(dem1));
//servo_day.write(80);
//delay(3000);
}
for (int i = 80; i >= 0; i--)
{
servo_day.write(i);
delay(50);
}
servo_day.write(80);
delay(3000);
}
}