-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c_motor_pi_to_nano.ino
More file actions
68 lines (60 loc) · 1.55 KB
/
i2c_motor_pi_to_nano.ino
File metadata and controls
68 lines (60 loc) · 1.55 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
65
66
67
68
#include <Wire.h>
#define SLAVE_ADDRESS 0x04
int number = 0;
int state = 0;
// MOTOR
int mot2 = 11, mot21 = 6;
int mot1 = 3, mot11 = 4;
int spd1 = 0, spd2 = 0,f;
void setup() {
Serial.begin(9600); // start serial for output
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
// define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
pinMode(mot1, OUTPUT); pinMode(mot11, OUTPUT);
pinMode(mot2, OUTPUT); pinMode(mot21, OUTPUT);
Serial.println("Ready!");
}
void loop() {
}
// callback for received data
void receiveData(int byteCount)
{
while (Wire.available())
{
Serial.print(" spd1=");
Serial.print(spd1);
Serial.print(" spd2=");
Serial.print(spd2);
Serial.print(" f=");
Serial.println(f);
if (f == 0)
{
spd1 = Wire.read();
f = 1;
}
else
{
spd2 = Wire.read();
f = 0;
}
}
}
void sendData() { Wire.write(f);}
void applyspeed()
{
switch (spd1)
{
case -255 ... -1: analogWrite(mot1, -spd1); digitalWrite(mot11 , HIGH); break;
case 1 ... 255: analogWrite(mot1, spd1); digitalWrite(mot11 , LOW); break;
default: analogWrite(mot1, 0); digitalWrite(mot11 , LOW); break;
}
switch (spd2)
{
case -255 ... -1: analogWrite(mot2, -spd2); digitalWrite(mot21 , LOW); break;
case 1 ... 255: analogWrite(mot2, spd2); digitalWrite(mot21 , HIGH); break;
default: analogWrite(mot2, 0); digitalWrite(mot21 , LOW); break;
}
}