Components and supplies
Arduino® UNO R4 WiFi
X-NUCLEO-53L5A1
Apps and platforms
Arduino IDE 1.8.19
NanoEdge AI Studio
Project description
Code
data logger
c
data logger code
1/* ============= 2 Copyright (c) 2024, STMicroelectronics 3 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that 7 the following conditions are met: 8 9 Redistributions of source code must retain the above copyright notice, this list of conditions and the 10 following disclaimer. 11 12 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 13 following disclaimer in the documentation and/or other materials provided with the distribution. 14 15 Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote 16 products derived from this software without specific prior written permission. 17 18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER / OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 24 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.* 25*/ 26 27/* If you want to use NEAI functions please, include NEAI library 28 in your Arduino libraries then, uncomment NEAI parts in the following code 29*/ 30 31/* Libraries part */ 32#include <Wire.h> 33#include <SparkFun_VL53L5CX_Library.h> 34// #include "NanoEdgeAI.h" 35// #include "knowledge.h" 36 37/* Macros definitions */ 38#define SERIAL_BAUD_RATE 115200 39 40/* Frame resolution: 4x4 or 8x8 */ 41#define SENSOR_FRAME_RESOLUTION 64 42 43/* Number of successive frames to be collected */ 44#define SENSOR_FRAMES 1 45 46/* Sensor data rate */ 47#define SENSOR_DATA_RATE 15 48 49/* NanoEdgeAI defines part 50 NEAI_MODE = 1: NanoEdgeAI functions = AI Mode. 51 NEAI_MODE = 0: Datalogging mode. 52*/ 53#define NEAI_MODE 0 54 55/* Depending on your target, you need to adapt 56 the i2c connection. For example, you have to use 57 "Wire" with Arduino Uno R4 Wifi but, "Wire1" with 58 the Arduino Giga R1 board. Check your hardware ! 59*/ 60#define i2c_comm Wire 61 62/* In this example, we use I2C connection */ 63SparkFun_VL53L5CX myImager; 64VL53L5CX_ResultsData measurementData; 65 66/* Global variables definitions */ 67static uint16_t neai_ptr = 0; 68static float neai_buffer[SENSOR_FRAME_RESOLUTION * SENSOR_FRAMES] = {0.0}; 69 70/* NEAI library variables */ 71// const char *id2class[CLASS_NUMBER + 1] = { 72// "unknown", 73// "nothing", 74// "scissors", 75// "paper", 76// "rock", 77// }; 78// static uint8_t neai_code = 0; 79// static uint16_t id_class = 0; 80// static float output_class_buffer[CLASS_NUMBER]; 81 82/* Initialization function: In this function, 83 code runs only once at boot / reset. 84*/ 85void setup() { 86 /* Init serial at baud rate 115200 */ 87 Serial.begin(SERIAL_BAUD_RATE); 88 89 /* I2C workaround: Sometimes, on some boards, 90 I2C get stuck after software reboot, reset so, 91 to avoid this, we toggle I2C clock pin at boot. 92 */ 93 pinMode(SCL, OUTPUT); 94 for (uint8_t i = 0; i < 20; i++) { 95 digitalWrite(SCL, !digitalRead(SCL)); 96 delay(1); 97 } 98 delay(100); 99 100 /* Init I2C connection between board & sensor */ 101 i2c_comm.begin(); 102 i2c_comm.setClock(400000); 103 if (myImager.begin() == false) { 104 Serial.print("Sensor not found - check your wiring. Freezing.\n"); 105 while (1); 106 } 107 108 /* Init VL53L5CX sensor settings: resolution & data rate */ 109 myImager.setResolution(SENSOR_FRAME_RESOLUTION); 110 myImager.setRangingFrequency(SENSOR_DATA_RATE); 111 myImager.startRanging(); 112 113 /* Initialize NanoEdgeAI AI */ 114 // neai_code = neai_classification_init(knowledge); 115 // if(neai_code != NEAI_OK) { 116 // Serial.print("Not supported board.\n"); 117 // } 118} 119 120/* Main function: Code run indefinitely */ 121void loop() { 122 while (neai_ptr < SENSOR_FRAMES) { 123 /* Check if new data if available */ 124 if (myImager.isDataReady() == true) { 125 /* If new data is available we read it ! */ 126 myImager.getRangingData(&measurementData); 127 /* Get data in the neai buffer */ 128 for (uint16_t i = 0; i < SENSOR_FRAME_RESOLUTION; i++) { 129 neai_buffer[i + (SENSOR_FRAME_RESOLUTION * neai_ptr)] = (float) measurementData.distance_mm[i]; 130 } 131 neai_ptr++; 132 } 133 134 } 135 /* Reset pointer */ 136 neai_ptr = 0; 137 138 /* Depending on NEAI_MODE value, run NanoEdge AI functions 139 or print accelerometer data to the serial (datalogging) 140 */ 141 // if(NEAI_MODE) { 142 // neai_classification(neai_buffer, output_class_buffer, &id_class); 143 // Serial.print((String)"Class: " + id2class[id_class] + ".\n"); 144 // } 145 // else { 146 /* Print the whole buffer to the serial */ 147 148 // } 149 for (uint16_t i = 0; i < (uint16_t) (SENSOR_FRAME_RESOLUTION * SENSOR_FRAMES); i++) { 150 Serial.print((String) neai_buffer[i] + " "); 151 } 152 Serial.print("\n"); 153 /* Clean neai buffer */ 154 memset(neai_buffer, 0.0, (uint16_t) (SENSOR_FRAME_RESOLUTION * SENSOR_FRAMES) * sizeof(float)); 155 delay(10); 156}
main code
c
main code
1/* Libraries part */ 2#include <Wire.h> 3#include <SparkFun_VL53L5CX_Library.h> 4#include "NanoEdgeAI.h" 5#include "knowledge.h" 6 7/* Macros definitions */ 8#define SERIAL_BAUD_RATE 115200 9 10/* Frame resolution: 4x4 or 8x8 */ 11#define SENSOR_FRAME_RESOLUTION 64 12 13/* Number of successive frames to be collected */ 14#define SENSOR_FRAMES 1 15 16/* Sensor data rate */ 17#define SENSOR_DATA_RATE 15 18 19/* NanoEdgeAI defines part 20 NEAI_MODE = 1: NanoEdgeAI functions = AI Mode. 21 NEAI_MODE = 0: Datalogging mode. 22*/ 23 24/* Depending on your target, you need to adapt 25 the i2c connection. For example, you have to use 26 "Wire" with Arduino Uno R4 Wifi but, "Wire1" with 27 the Arduino Giga R1 board. Check your hardware ! 28*/ 29#define i2c_comm Wire 30 31/* In this example, we use I2C connection */ 32SparkFun_VL53L5CX myImager; 33VL53L5CX_ResultsData measurementData; 34 35/* variable to play rock paper scissors */ 36static uint16_t player_score = 0; 37static uint16_t arduino_score = 0; 38static uint16_t points_to_win = 3; 39 40 41/* Global variables definitions */ 42static uint16_t neai_ptr = 0; 43static float neai_buffer[SENSOR_FRAME_RESOLUTION * SENSOR_FRAMES] = {0.0}; 44 45/* NEAI library variables */ 46static uint8_t neai_code = 0; 47static uint16_t id_class = 0; 48static float output_class_buffer[CLASS_NUMBER]; 49const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name 50 "unknown", 51 "nothing", 52 "scissors", 53 "rock", 54 "paper", 55}; 56 57void fill_buffer(); 58 59/* Initialization function: In this function, 60 code runs only once at boot / reset. 61*/ 62void setup() { 63 /* Init serial at baud rate 115200 */ 64 Serial.begin(SERIAL_BAUD_RATE); 65 66 /* I2C workaround: Sometimes, on some boards, 67 I2C get stuck after software reboot, reset so, 68 to avoid this, we toggle I2C clock pin at boot. 69 */ 70 pinMode(SCL, OUTPUT); 71 for (uint8_t i = 0; i < 20; i++) { 72 digitalWrite(SCL, !digitalRead(SCL)); 73 delay(1); 74 } 75 delay(100); 76 77 /* Init I2C connection between board & sensor */ 78 i2c_comm.begin(); 79 i2c_comm.setClock(400000); 80 if (myImager.begin() == false) { 81 Serial.print("Sensor not found - check your wiring. Freezing.\n"); 82 while (1); 83 } 84 85 /* Init VL53L5CX sensor settings: resolution & data rate */ 86 myImager.setResolution(SENSOR_FRAME_RESOLUTION); 87 myImager.setRangingFrequency(SENSOR_DATA_RATE); 88 myImager.startRanging(); 89 90 /* Initialize NanoEdgeAI AI */ 91 neai_code = neai_classification_init(knowledge); 92 if (neai_code != NEAI_OK) { 93 Serial.print("Not supported board.\n"); 94 } 95} 96 97/* Main function: Code run indefinitely */ 98void loop() { 99 100 Serial.println("The game is starting, be ready"); 101 delay(1000); 102 103 while ((arduino_score < points_to_win) and (player_score < points_to_win)) { 104 Serial.println(); 105 Serial.print("Scissor"); 106 delay(1000); 107 Serial.print(" Paper"); 108 delay(1000); 109 Serial.print(" Rock!!"); 110 Serial.println(); 111 delay(1000); 112 113 114 fill_buffer(); 115 //classification 116 neai_classification(neai_buffer, output_class_buffer, &id_class); 117 118 while (id_class == 1) { 119 //collect tof data for classification 120 fill_buffer(); 121 //classification 122 neai_classification(neai_buffer, output_class_buffer, &id_class); 123 Serial.println("You didn't make a sign, play again"); 124 delay(1000); 125 126 } 127 128 129 //pick a sign played by the board 130 int random_play = random(2, 5); 131 132 133 Serial.print("you played: "); 134 Serial.println(id2class[id_class]); 135 Serial.print("Arduino played: "); 136 Serial.println(id2class[random_play]); 137 delay(100); 138 139 //comparison 140 if (id_class == random_play) { 141 Serial.println("Draw, play again!"); 142 } else { 143 if ( 144 // scisoor vs paper 145 (id_class == 2 && random_play == 4) or 146 // rock vs scissors 147 (id_class == 3 && random_play == 2) or 148 //paper vs rock 149 (id_class == 4 && random_play == 3) 150 ) { 151 Serial.println("You won"); 152 player_score ++; 153 } else { 154 Serial.println("You loose"); 155 arduino_score ++; 156 } 157 Serial.print("Player Score: "); 158 Serial.println(player_score); 159 Serial.print("Arduino Score: "); 160 Serial.println(arduino_score); 161 delay(1000); 162 } 163 164 /* Clean neai buffer */ 165 memset(neai_buffer, 0.0, (uint16_t) (SENSOR_FRAME_RESOLUTION * SENSOR_FRAMES) * sizeof(float)); 166 } 167 Serial.println(" "); 168 if (player_score > arduino_score) { 169 Serial.println("RESULTS: Well done, you won"); 170 } else { 171 Serial.println("RESULTS:Too bad, you lost"); 172 } 173 Serial.println(" "); 174 175 arduino_score = 0; 176 player_score = 0; 177 delay(1000); 178} 179 180 181void fill_buffer() { 182 while (neai_ptr < SENSOR_FRAMES) { 183 /* Check if new data if available */ 184 if (myImager.isDataReady() == true) { 185 /* If new data is available we read it ! */ 186 myImager.getRangingData(&measurementData); 187 /* Get data in the neai buffer */ 188 for (uint16_t i = 0; i < SENSOR_FRAME_RESOLUTION; i++) { 189 neai_buffer[i + (SENSOR_FRAME_RESOLUTION * neai_ptr)] = (float) measurementData.distance_mm[i]; 190 } 191 neai_ptr++; 192 } 193 } 194 /* Reset pointer */ 195 neai_ptr = 0; 196}
Downloadable files
data logger
ino code for step data logging
arduino-rock-paper-scissors-datalogger.ino
main code
final code with rock paper scissor game
arduino-rock-paper-scissors-main.ino
Comments
Only logged in users can leave comments
bob-easyeda
2 months ago
Hi, your project is so amazing! Are you interested in OSHWLab Stars :https://oshwlab.com/activities/spark2023 we are offering up to $5000 in coupons for each entry to cover all your fabrication costs.