-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcooki_motor.ino
More file actions
64 lines (54 loc) · 2.17 KB
/
cooki_motor.ino
File metadata and controls
64 lines (54 loc) · 2.17 KB
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
// ─────────────────────────────────────────────────────────────────────────────
// COOKi Motor Controller (Arduino Uno / Nano)
// ─────────────────────────────────────────────────────────────────────────────
//
// Serial input: "speed,direction\n" e.g. "200,1\n"
// speed: 0-255 (PWM duty cycle)
// direction: 0=CCW 1=CW
//
// Serial reply: "OK spd=200 dir=1"
//
// Wiring:
// PWM_PIN (D9) → motor driver IN1 / PWM input
// DIR_PIN (D8) → motor driver IN2 / direction input
// Motor driver GND → Arduino GND
// Motor driver VM → external power (6-12V for the motor)
//
// Compatible motor drivers: L298N, L9110S, DRV8833, TB6612FNG
// ─────────────────────────────────────────────────────────────────────────────
const int PWM_PIN = 9;
const int DIR_PIN = 8;
const int LED_PIN = 13; // built-in LED blinks on each command
void setup() {
Serial.begin(9600);
pinMode(PWM_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
analogWrite(PWM_PIN, 0);
digitalWrite(DIR_PIN, LOW);
Serial.println("COOKi motor ready");
}
void loop() {
if (!Serial.available()) return;
String cmd = Serial.readStringUntil('\n');
cmd.trim();
int comma = cmd.indexOf(',');
if (comma == -1) {
Serial.println("ERR bad format — expected speed,direction");
return;
}
int spd = cmd.substring(0, comma).toInt();
int dir = cmd.substring(comma + 1).toInt();
spd = constrain(spd, 0, 255);
dir = constrain(dir, 0, 1);
digitalWrite(DIR_PIN, dir);
analogWrite(PWM_PIN, spd);
// Blink LED on command received
digitalWrite(LED_PIN, HIGH);
delay(20);
digitalWrite(LED_PIN, LOW);
Serial.print("OK spd=");
Serial.print(spd);
Serial.print(" dir=");
Serial.println(dir);
}