1
+ /*
2
+ This file is part of the Arduino_Alvik library.
3
+
4
+ Copyright (c) 2024 Arduino SA
5
+
6
+ This Source Code Form is subject to the terms of the Mozilla Public
7
+ License, v. 2.0. If a copy of the MPL was not distributed with this
8
+ file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
+
10
+ */
11
+
12
+ // This example shows how to interface 2 Alvik robots via ESPnow.
13
+ // At startup, you can select if an Alvik is a trasmitter by pressing the "check button" or a receiver by pressing "cancel button". Use arrows to move the robot.
14
+
15
+ #include " Arduino_Alvik.h"
16
+ #include < esp_now.h>
17
+ #include < WiFi.h>
18
+
19
+ Arduino_Alvik alvik;
20
+
21
+ uint8_t broadcastAddress[] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
22
+ uint8_t myData;
23
+ esp_now_peer_info_t peerInfo;
24
+
25
+
26
+ int alvik_mode = -1 ; // 0 is receiver, 1 is sender
27
+
28
+ bool led_blink = false ;
29
+
30
+
31
+ void setup () {
32
+ Serial.begin (115200 );
33
+ while ((!Serial)&&(millis ()>3000 ));
34
+
35
+ alvik.begin ();
36
+
37
+ WiFi.mode (WIFI_STA);
38
+ if (esp_now_init () != ESP_OK) {
39
+ Serial.println (" Error initializing ESP-NOW" );
40
+ return ;
41
+ }
42
+
43
+ while (alvik_mode == -1 ){
44
+ if (alvik.get_touch_cancel ()){
45
+ alvik_mode = 0 ;
46
+ }
47
+ if (alvik.get_touch_ok ()){
48
+ alvik_mode = 1 ;
49
+ }
50
+ }
51
+ if (alvik_mode == 0 ){
52
+ esp_now_register_recv_cb (OnDataRecv);
53
+ }
54
+ else {
55
+ memcpy (peerInfo.peer_addr , broadcastAddress, 6 );
56
+ peerInfo.channel = 0 ;
57
+ peerInfo.encrypt = false ;
58
+
59
+ if (esp_now_add_peer (&peerInfo) != ESP_OK){
60
+ Serial.println (" Failed to add peer" );
61
+ return ;
62
+ }
63
+ }
64
+ }
65
+
66
+ void loop () {
67
+ if (alvik_mode==0 ){
68
+ alvik.left_led .set_color (led_blink, !led_blink, 0 );
69
+ alvik.right_led .set_color (!led_blink, led_blink, 0 );
70
+ delay (500 );
71
+ }
72
+ else {
73
+ if (alvik.get_touch_any ()){
74
+ if (alvik.get_touch_up ()){
75
+ myData = ' F' ;
76
+ esp_now_send (broadcastAddress, (uint8_t *) &myData, sizeof (myData));
77
+ }
78
+ if (alvik.get_touch_down ()){
79
+ myData = ' B' ;
80
+ esp_now_send (broadcastAddress, (uint8_t *) &myData, sizeof (myData));
81
+ }
82
+ if (alvik.get_touch_left ()){
83
+ myData = ' L' ;
84
+ esp_now_send (broadcastAddress, (uint8_t *) &myData, sizeof (myData));
85
+ }
86
+ if (alvik.get_touch_right ()){
87
+ myData = ' R' ;
88
+ esp_now_send (broadcastAddress, (uint8_t *) &myData, sizeof (myData));
89
+ }
90
+ if (alvik.get_touch_center ()){
91
+ myData = ' S' ;
92
+ esp_now_send (broadcastAddress, (uint8_t *) &myData, sizeof (myData));
93
+ }
94
+ }
95
+ alvik.left_led .set_color (0 , 0 , led_blink);
96
+ alvik.right_led .set_color (0 , 0 , led_blink);
97
+ delay (100 );
98
+ }
99
+ led_blink = !led_blink;
100
+ }
101
+
102
+ void OnDataRecv (const uint8_t * mac, const uint8_t *incomingData, int len){
103
+ Serial.print (incomingData[0 ]);
104
+ switch (incomingData[0 ]){
105
+ case ' F' :
106
+ alvik.drive (7 , 0 );
107
+ break ;
108
+ case ' B' :
109
+ alvik.drive (-7 , 0 );
110
+ break ;
111
+ case ' L' :
112
+ alvik.drive (0 , 45 );
113
+ break ;
114
+ case ' R' :
115
+ alvik.drive (0 , -45 );
116
+ break ;
117
+ case ' S' :
118
+ alvik.brake ();
119
+ break ;
120
+ }
121
+ Serial.println ();
122
+ }
0 commit comments