Posts with «best arduino blog» label

Send HEX values to Arduino

FIVE MINUTE TUTORIAL

Project Description: Sending Hex values to an Arduino UNO


This simple tutorial will show you how to send Hexadecimal values from a computer to an Arduino Uno. The "Processing" programming language will be used to send the HEX values from the computer when a mouse button is pressed. The Arduino will use these values to adjust the brightness of an LED.



 

Learning Objectives


  • To Send Hexadecimal (Hex) values from a computer to the Arduino
  • Trigger an action based on the press of a mouse button
  • Learn to create a simple Computer to Arduino interface
  • Use Arduino's PWM capabilities to adjust brightness of an LED
  • Learn to use Arduino's analogWrite() function
  • Create a simple LED circuit


 

Parts Required:


Fritzing Sketch


The diagram below will show you how to connect an LED to Digital Pin 10 on the Arduino.
Don't forget the 330 ohm resistor !
 


 
 

Arduino Sketch


The latest version of Arduino IDE can be downloaded here.
 
  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
/* ==================================================================================================================================================
         Project: 5 min tutorial: Send Hex from computer to Arduino
          Author: Scott C
         Created: 21th June 2015
     Arduino IDE: 1.6.4
         Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
     Description: Arduino Sketch used to adjust the brightness of an LED based on the values received
                  on the serial port. The LED needs to be connected to a PWM pin. In this sketch
                  Pin 10 is used, however you could use Pin 3, 5, 6, 9, or 11 - if you are using an Arduino Uno.
===================================================================================================================================================== */

byte byteRead; //Variable used to store the byte received on the Serial Port
int ledPin = 10; //LED is connected to Arduino Pin 10. This pin must be PWM capable.

void setup() {
 Serial.begin(9600); //Initialise Serial communication with the computer
 pinMode(ledPin, OUTPUT); //Set Pin 10 as an Output pin
 byteRead = 0;                   //Initialise the byteRead variable to zero.
}

void loop() {
  if(Serial.available()) {
    byteRead = Serial.read(); //Update the byteRead variable with the Hex value received on the Serial COM port.
  }
  
  analogWrite(ledPin, byteRead); //Use PWM to adjust the brightness of the LED. Brightness is determined by the "byteRead" variable.
}


 


 
 

Processing Sketch


The latest version of the Processing IDE can be downloaded here.
 
  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
/* ==================================================================================================================================================
         Project: 5 min tutorial: Send Hex from computer to Arduino
          Author: Scott C
         Created: 21th June 2015
  Processing IDE: 2.2.1
         Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
     Description: Processing Sketch used to send HEX values from computer to Arduino when the mouse is pressed.
                  The alternating values 0xFF and 0x00 are sent to the Arduino Uno to turn an LED on and off.
                  You can send any HEX value from 0x00 to 0xFF. This sketch also shows how to convert Hex strings
                  to Hex numbers.
===================================================================================================================================================== */

import processing.serial.*; //This import statement is required for Serial communication

Serial comPort;                       //comPort is used to write Hex values to the Arduino
boolean toggle = false; //toggle variable is used to control which hex variable to send
String zeroHex = "00"; //This "00" string will be converted to 0x00 and sent to Arduino to turn LED off.
String FFHex = "FF"; //This "FF" string will be converted to 0xFF and sent to Arduino to turn LED on.

void setup(){
    comPort = new Serial(this, Serial.list()[0], 9600); //initialise the COM port for serial communication at a baud rate of 9600.
    delay(2000);                      //this delay allows the com port to initialise properly before initiating any communication.
    background(0); //Start with a black background.
    
}


void draw(){ //the draw() function is necessary for the sketch to compile
    //do nothing here //even though it does nothing.
}


void mousePressed(){ //This function is called when the mouse is pressed within the Processing window.
  toggle = ! toggle;                   //The toggle variable will change back and forth between "true" and "false"
  if(toggle){ //If the toggle variable is TRUE, then send 0xFF to the Arduino
     comPort.write(unhex(FFHex)); //The unhex() function converts the "FF" string to 0xFF
     background(0,0,255); //Change the background colour to blue as a visual indication of a button press.
  } else {
    comPort.write(unhex(zeroHex)); //If the toggle variable is FALSE, then send 0x00 to the Arduino
    background(0); //Change the background colour to black as a visual indication of a button press.
  }
}


 

The Video


 

The tutorial above is a quick demonstration of how to convert Hex strings on your computer and send them to an Arduino. The Arduino can use the values to change the brightness of an LED as shown in this tutorial, however you could use it to modify the speed of a motor, or to pass on commands to another module. Hopefully this short tutorial will help you with your project. Please let me know how it helped you in the comments below.

 
 



If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.


 
 
             


 
 



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

CH376S USB Read/Write module

Have you ever wondered if there was a way to store and retrieve data from a USB stick with an Arduino UNO? Most people choose SD cards to store their project data, but you may be surprised there IS a way!
IC Station have a nice little module which allows you store and retrieve your Arduino (or other MCU) project data to a USB stick.
 
I am not too sure why USB storage is not widely used in Arduino projects? These modules are not expensive, they have been around for quite a while, and are relatively simple to use. You do not need any libraries to get them to work, however, I must say that documentation for this module is not that easy to find. This site and this document proved to be very useful in my endevour to get this module working, and I hope my tutorial below will help you get started and bridge some of the information gaps.
 
The "CH376S USB read/write module" has a CH376S chip onboard which does most of the hard work for you. All you have to do is send the module some commands from the Arduino and the CH376S chip will do the rest. You can communicate with the module in three different ways:

  • Parallel communication
  • SPI communication
  • and Serial (UART) communication.

This project will show you the connections and code for the Serial (UART) communication method only.


 

Parts Required:

Remove the Jumper

When the CH376S USB module arrives in it's package, it will have a jumper between the TXD pin and GND. You will need to remove this jumper to make the necessary connections between the Arduino UNO and the CH376S USB module.


 

Fritzing Sketch

Please note, that the Arduino Sketch makes use of the Arduino UNO's onboard LED on digital pin 13. The Fritzing sketch below shows an LED + 300 ohm resistor on a breadboard. This is optional. The LED is not a necessary component of CH376S module communication.

Also be aware that the CH376S USB module has an onboard LED just above the TXD and GND pins near the USB port. This LED will only turn on providing the CH376S module is in USB mode AND a USB device has been inserted into the USB port. Both conditions must be met before the module's onboard LED will illuminate. You will not see the LED turn on just by powering the board.
 
The wire diagram below is the correct setup for Serial communication between an Arduino UNO and the CH376S module. If you wish to use SPI or Parallel communication, you will need to refer to the datasheet.


 
 

Arduino Sketch


 
  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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505

/* ===============================================================
      Project: CH376S USB Read/Write Module testing ground
       Author: Scott C
      Created: 1st May 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This project will allow you to perform many of the functions available on the CH376S module.
               Checking connection to the module, putting the module into USB mode, resetting the module, 
               reading, writing, appending text to files on the USB stick. This is very useful alternative to
               SD card modules, plus it doesn't need any libraries.
================================================================== */

#include <SoftwareSerial.h>

byte computerByte; //used to store data coming from the computer
byte USB_Byte; //used to store data coming from the USB stick
int LED = 13; //the LED is connected to digital pin 13
int timeOut = 2000; //TimeOut is 2 seconds. This is the amount of time you wish to wait for a response from the CH376S module.
String wrData = "What is the meaning of life ?"; //We will write this data to a newly created file.
String wrData2 = "42"; //We will append this data to an already existing file.

SoftwareSerial USB(10, 11); // Digital pin 10 on Arduino (RX) connects to TXD on the CH376S module
                                                      // Digital pin 11 on Arduino (TX) connects to RXD on the CH376S module
                                                      // GND on Arduino to GND on CH376S module
                                                      // 5V on Arduino to 5V on CH376S module
//==============================================================================================================================================
void setup() {
  Serial.begin(9600); // Setup serial communication with the computer (using a baud rate of 9600 on serial monitor)
  USB.begin(9600); // Setup serial communication with the CH376S module (using the default baud rate of 9600)
  pinMode(LED,OUTPUT); // Define digital pin 13 as an OUTPUT pin - so that we can use it with an LED
  digitalWrite(LED,LOW); // Turn off the LED
}

//================================================================================================================================================
void loop() {
  if(Serial.available()){
    computerByte = Serial.read(); //read any incoming bytes from the Serial monitor, and store this byte in the variable called computerByte
    if(computerByte==49){ //1 //If you send the number 1 from the serial monitor, the arduino will read it as digital number 49. Google "ascii table" for more info.
      printCommandHeader("COMMAND1: CHECK CONNECTION");
      checkConnection(0x01);                           // Check for successful connection and communication with the CH376S module.
    } 
    if(computerByte==50){ //2
     printCommandHeader("COMMAND2: set_USB_Mode");
      set_USB_Mode(0x06);                              // Code used to enable read/write communication and monitoring of the USB stick
    }
    if(computerByte==51){ //3
      printCommandHeader("COMMAND3: resetALL");
      resetALL();                                      // Reset the USB device
    }
    if(computerByte==52){ //4
      printCommandHeader("COMMAND4: Create and Write to File : TEST4.TXT");
      writeFile("TEST4.TXT", wrData); // Create a file called TEST4.TXT and then Write the contents of wrData to this file
    }
    if(computerByte==53){ //5
      printCommandHeader("COMMAND5: Read File: TEST4.TXT");
      readFile("TEST4.TXT"); // Read the contents of this file on the USB disk, and display contents in the Serial Monitor
    }
    if(computerByte==54){ //6
      printCommandHeader("COMMAND6: Append data to file: TEST4.TXT");
      appendFile("TEST4.TXT", wrData2); // Append data to the end of the file.
    }
    if(computerByte==55){ //7
      printCommandHeader("COMMAND7: Delete File: TEST4.TXT");
      fileDelete("TEST4.TXT"); // Delete the file named TEST4.TXT
    }
    if(computerByte==56){ //8
      printCommandHeader("COMMAND8: Read File: TEST2.TXT");
      readFile("TEST2.TXT"); // Read the contents of the TEST2.TXT file on the USB disk, and display contents in the Serial Monitor
    }
    if(computerByte==57){ //9
      printCommandHeader("COMMAND9: Read File: TEST3.TXT");
      readFile("TEST3.TXT"); // Read the contents of the TEST3.TXT file on the USB disk, and display contents in the Serial Monitor
    }
  }
  
  if(USB.available()){ // This is here to capture any unexpected data transmitted by the CH376S module
    Serial.print("CH376S has just sent this code:");
    Serial.println(USB.read(), HEX);
  }
}

//END OF LOOP FUNCTION ========================================================================================================================================

//print Command header
void printCommandHeader(String header){
   Serial.println("======================");
   Serial.println("");
   Serial.println(header);
   Serial.println("----------------------");
}

//checkConnection==================================================================================
//This function is used to check for successful communication with the CH376S module. This is not dependant of the presence of a USB stick.
//Send any value between 0 to 255, and the CH376S module will return a number = 255 - value. 
void checkConnection(byte value){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x06);
  USB.write(value);
  
  if(waitForResponse("checking connection")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==(255-value)){
       Serial.println(">Connection to CH376S was successful.");
       blinkLED();                               //blink the LED for 1 second if the connection was successful
    } else {
      Serial.print(">Connection to CH376S - FAILED.");
    }
  }
}

//set_USB_Mode=====================================================================================
//Make sure that the USB is inserted when using 0x06 as the value in this specific code sequence
void set_USB_Mode (byte value){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x15);
  USB.write(value);
  
  delay(20);
  
  if(USB.available()){
    USB_Byte=USB.read();
    //Check to see if the command has been successfully transmitted and acknowledged.
    if(USB_Byte==0x51){ // If true - the CH376S has acknowledged the command.
        Serial.println("set_USB_Mode command acknowledged"); //The CH376S will now check and monitor the USB port
        USB_Byte = USB.read();
        
        //Check to see if the USB stick is connected or not.
        if(USB_Byte==0x15){ // If true - there is a USB stick connected
          Serial.println("USB is present");
          blinkLED();                                     // If the process was successful, then turn the LED on for 1 second
        } else {
          Serial.print("USB Not present. Error code:"); // If the USB is not connected - it should return an Error code = FFH
          Serial.print(USB_Byte, HEX);
          Serial.println("H");
        }
        
    } else {
        Serial.print("CH3765 error! Error code:");
        Serial.print(USB_Byte, HEX);
        Serial.println("H");
    }   
  }
  delay(20);
}

//resetALL=========================================================================================
//This will perform a hardware reset of the CH376S module - which usually takes about 35 msecs =====
void resetALL(){
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x05);
    Serial.println("The CH376S module has been reset !");
    delay(200);
}

//readFile=====================================================================================
//This will send a series of commands to read data from a specific file (defined by fileName)
void readFile(String fileName){
  resetALL();                     //Reset the module
  set_USB_Mode(0x06);             //Set to USB Mode
  diskConnectionStatus();         //Check that communication with the USB device is possible
  USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
  setFileName(fileName);          //Set File name
  fileOpen();                     //Open the file for reading
  int fs = getFileSize(); //Get the size of the file
  fileRead();                     //***** Send the command to read the file ***
  fileClose(0x00);                //Close the file
}

//writeFile========================================================================================
//is used to create a new file and then write data to that file. "fileName" is a variable used to hold the name of the file (e.g TEST.TXT). "data" should not be greater than 255 bytes long. 
void writeFile(String fileName, String data){
  resetALL();                     //Reset the module
  set_USB_Mode(0x06);             //Set to USB Mode
  diskConnectionStatus();         //Check that communication with the USB device is possible
  USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
  setFileName(fileName);          //Set File name
  if(fileCreate()){ //Try to create a new file. If file creation is successful
    fileWrite(data);              //write data to the file.
  } else {
    Serial.println("File could not be created, or it already exists");
  }
  fileClose(0x01);
}

//appendFile()====================================================================================
//is used to write data to the end of the file, without erasing the contents of the file.
void appendFile(String fileName, String data){
    resetALL();                     //Reset the module
    set_USB_Mode(0x06);             //Set to USB Mode
    diskConnectionStatus();         //Check that communication with the USB device is possible
    USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
    setFileName(fileName);          //Set File name
    fileOpen();                     //Open the file
    filePointer(false); //filePointer(false) is to set the pointer at the end of the file. filePointer(true) will set the pointer to the beginning.
    fileWrite(data);                //Write data to the end of the file
    fileClose(0x01);                //Close the file using 0x01 - which means to update the size of the file on close.
}
  
//setFileName======================================================================================
//This sets the name of the file to work with
void setFileName(String fileName){
  Serial.print("Setting filename to:");
  Serial.println(fileName);
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x2F);
  USB.write(0x2F); // Every filename must have this byte to indicate the start of the file name.
  USB.print(fileName); // "fileName" is a variable that holds the name of the file. eg. TEST.TXT
  USB.write((byte)0x00); // you need to cast as a byte - otherwise it will not compile. The null byte indicates the end of the file name.
  delay(20);
}

//diskConnectionStatus================================================================================
//Check the disk connection status
void diskConnectionStatus(){
  Serial.println("Checking USB disk connection status");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x30);

  if(waitForResponse("Connecting to USB disk")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful
       Serial.println(">Connection to USB OK");
    } else {
      Serial.print(">Connection to USB - FAILED.");
    }
  }
}

//USBdiskMount========================================================================================
//initialise the USB disk and check that it is ready - this process is required if you want to find the manufacturing information of the USB disk
void USBdiskMount(){
  Serial.println("Mounting USB disk");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x31);

  if(waitForResponse("mounting USB disk")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful
       Serial.println(">USB Mounted - OK");
    } else {
      Serial.print(">Failed to Mount USB disk.");
    }
  }
}

//fileOpen========================================================================================
//opens the file for reading or writing
void fileOpen(){
  Serial.println("Opening file.");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x32);
  if(waitForResponse("file Open")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful
       Serial.println(">File opened successfully.");
    } else {
      Serial.print(">Failed to open file.");
    }
  }
}

//setByteRead=====================================================================================
//This function is required if you want to read data from the file. 
boolean setByteRead(byte numBytes){
  boolean bytesToRead=false;
  int timeCounter = 0;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3A);
  USB.write((byte)numBytes); //tells the CH376S how many bytes to read at a time
  USB.write((byte)0x00);
  if(waitForResponse("setByteRead")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x1D){ //read the CH376S message. If equal to 0x1D, data is present, so return true. Will return 0x14 if no data is present.
      bytesToRead=true;
    }
  }
  return(bytesToRead);


//getFileSize()===================================================================================
//writes the file size to the serial Monitor.
int getFileSize(){
  int fileSize=0;
  Serial.println("Getting File Size");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x0C);
  USB.write(0x68);
  delay(100);
  Serial.print("FileSize =");
  if(USB.available()){
    fileSize = fileSize + USB.read();
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255);
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255*255);
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255*255*255);
  }     
  Serial.println(fileSize);
  delay(10);
  return(fileSize);
}


//fileRead========================================================================================
//read the contents of the file
void fileRead(){
  Serial.println("Reading file:");
  byte firstByte = 0x00; //Variable to hold the firstByte from every transmission. Can be used as a checkSum if required.
  byte numBytes = 0x40; //The maximum value is 0x40 = 64 bytes
 
  while(setByteRead(numBytes)){ //This tells the CH376S module how many bytes to read on the next reading step. In this example, we will read 0x10 bytes at a time. Returns true if there are bytes to read, false if there are no more bytes to read.
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x27); //Command to read ALL of the bytes (allocated by setByteRead(x))
    if(waitForResponse("reading data")){ //Wait for the CH376S module to return data. TimeOut will return false. If data is being transmitted, it will return true.
        firstByte=USB.read(); //Read the first byte
        while(USB.available()){
          Serial.write(USB.read()); //Send the data from the USB disk to the Serial monitor
          delay(1); //This delay is necessary for successful Serial transmission
        }
    }
    if<!continueRead()){><span>//prepares the module for further reading. If false, stop reading.</span><br />      <span>break</span>; <span>//You need the continueRead() method if the data to be read from the USB device is greater than numBytes.</span><br />    }<br />  }<br />  <span><b>Serial</b></span>.<span>println</span>();<br />  <span><b>Serial</b></span>.<span>println</span>(<span>"NO MORE DATA"</span>);<br />}<br /><br /><span>//fileWrite=======================================================================================</span><br /><span>//are the commands used to write to the file</span><br /><span>void</span> fileWrite(<span>String</span> data){<br />  <span><b>Serial</b></span>.<span>println</span>(<span>"Writing to file:"</span>);<br />  <span>byte</span> dataLength = (<span>byte</span>) data.<span>length</span>(); <span>// This variable holds the length of the data to be written (in bytes)</span><br />  <span><b>Serial</b></span>.<span>println</span>(data);<br />  <span><b>Serial</b></span>.<span>print</span>(<span>"Data Length:"</span>);<br />  <span><b>Serial</b></span>.<span>println</span>(dataLength);<br />  <span>delay</span>(100);<br />  <span>// This set of commands tells the CH376S module how many bytes to expect from the Arduino. (defined by the "dataLength" variable)</span><br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x3C);<br />  USB.<span>write</span>((<span>byte</span>) dataLength);<br />  USB.<span>write</span>((<span>byte</span>) 0x00);<br />  <span>if</span>(waitForResponse(<span>"setting data Length"</span>)){ <span>// Wait for an acknowledgement from the CH376S module before trying to send data to it</span><br />    <span>if</span>(getResponseFromUSB()==0x1E){ <span>// 0x1E indicates that the USB device is in write mode.</span><br />      USB.<span>write</span>(0x57);<br />      USB.<span>write</span>(0xAB);<br />      USB.<span>write</span>(0x2D);<br />      USB.<span>print</span>(data); <span>// write the data to the file</span><br />  <br />      <span>if</span>(waitForResponse(<span>"writing data to file"</span>)){ <span>// wait for an acknowledgement from the CH376S module</span><br />      }<br />      <span><b>Serial</b></span>.<span>print</span>(<span>"Write code (normally FF and 14): "</span>);<br />      <span><b>Serial</b></span>.<span>print</span>(USB.<span>read</span>(),<span>HEX</span>); <span>// code is normally 0xFF</span><br />      <span><b>Serial</b></span>.<span>print</span>(<span>","</span>);<br />      USB.<span>write</span>(0x57);<br />      USB.<span>write</span>(0xAB);<br />      USB.<span>write</span>(0x3D); <span>// This is used to update the file size. Not sure if this is necessary for successful writing.</span><br />      <span>if</span>(waitForResponse(<span>"updating file size"</span>)){ <span>// wait for an acknowledgement from the CH376S module</span><br />      }<br />      <span><b>Serial</b></span>.<span>println</span>(USB.<span>read</span>(),<span>HEX</span>); <span>//code is normally 0x14</span><br />    }<br />  }<br />}<br /><br /><span>//continueRead()==================================================================================</span><br /><span>//continue to read the file : I could not get this function to work as intended.</span><br /><span>boolean</span> continueRead(){<br />  <span>boolean</span> readAgain = <span>false</span>;<br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x3B);<br />  <span>if</span>(waitForResponse(<span>"continueRead"</span>)){ <span>//wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.</span><br />     <span>if</span>(getResponseFromUSB()==0x14){ <span>//CH376S will send 0x14 if this command was successful</span><br />       readAgain=<span>true</span>;<br />     }<br />  }<br />  <span>return</span>(readAgain);<br />} <br /><br /><span>//fileCreate()========================================================================================</span><br /><span>//the command sequence to create a file</span><br /><span>boolean</span> fileCreate(){<br />  <span>boolean</span> createdFile = <span>false</span>;<br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x34);<br />  <span>if</span>(waitForResponse(<span>"creating file"</span>)){ <span>//wait for a response from the CH376S. If file has been created successfully, it will return true.</span><br />     <span>if</span>(getResponseFromUSB()==0x14){ <span>//CH376S will send 0x14 if this command was successful</span><br />       createdFile=<span>true</span>;<br />     }<br />  }<br />  <span>return</span>(createdFile);<br />}<br /><br /><br /><span>//fileDelete()========================================================================================</span><br /><span>//the command sequence to delete a file</span><br /><span>void</span> fileDelete(<span>String</span> fileName){<br />  setFileName(fileName);<br />  <span>delay</span>(20);<br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x35);<br />  <span>if</span>(waitForResponse(<span>"deleting file"</span>)){ <span>//wait for a response from the CH376S. If file has been created successfully, it will return true.</span><br />     <span>if</span>(getResponseFromUSB()==0x14){ <span>//CH376S will send 0x14 if this command was successful</span><br />       <span><b>Serial</b></span>.<span>println</span>(<span>"Successfully deleted file"</span>);<br />     }<br />  }<br />}<br />  <br /><br /><span>//filePointer========================================================================================</span><br /><span>//is used to set the file pointer position. true for beginning of file, false for the end of the file.</span><br /><span>void</span> filePointer(<span>boolean</span> fileBeginning){<br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x39);<br />  <span>if</span>(fileBeginning){<br />    USB.<span>write</span>((<span>byte</span>)0x00); <span>//beginning of file</span><br />    USB.<span>write</span>((<span>byte</span>)0x00);<br />    USB.<span>write</span>((<span>byte</span>)0x00);<br />    USB.<span>write</span>((<span>byte</span>)0x00);<br />  } <span>else</span> {<br />    USB.<span>write</span>((<span>byte</span>)0xFF); <span>//end of file</span><br />    USB.<span>write</span>((<span>byte</span>)0xFF);<br />    USB.<span>write</span>((<span>byte</span>)0xFF);<br />    USB.<span>write</span>((<span>byte</span>)0xFF);<br />  }<br />  <span>if</span>(waitForResponse(<span>"setting file pointer"</span>)){ <span>//wait for a response from the CH376S. </span><br />     <span>if</span>(getResponseFromUSB()==0x14){ <span>//CH376S will send 0x14 if this command was successful</span><br />       <span><b>Serial</b></span>.<span>println</span>(<span>"Pointer successfully applied"</span>);<br />     }<br />  }<br />}<br /><br /><br /><span>//fileClose=======================================================================================</span><br /><span>//closes the file</span><br /><span>void</span> fileClose(<span>byte</span> closeCmd){<br />  <span><b>Serial</b></span>.<span>println</span>(<span>"Closing file:"</span>);<br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x36);<br />  USB.<span>write</span>((<span>byte</span>)closeCmd); <span>// closeCmd = 0x00 = close without updating file Size, 0x01 = close and update file Size</span><br /><br />  <span>if</span>(waitForResponse(<span>"closing file"</span>)){ <span>// wait for a response from the CH376S. </span><br />     <span>byte</span> resp = getResponseFromUSB();<br />     <span>if</span>(resp==0x14){ <span>// CH376S will send 0x14 if this command was successful</span><br />       <span><b>Serial</b></span>.<span>println</span>(<span>">File closed successfully."</span>);<br />     } <span>else</span> {<br />       <span><b>Serial</b></span>.<span>print</span>(<span>">Failed to close file. Error code:"</span>);<br />       <span><b>Serial</b></span>.<span>println</span>(resp, <span>HEX</span>);<br />     }  <br />  }<br />}<br /><br /><span>//waitForResponse===================================================================================</span><br /><span>//is used to wait for a response from USB. Returns true when bytes become available, false if it times out.</span><br /><span>boolean</span> waitForResponse(<span>String</span> errorMsg){<br />  <span>boolean</span> bytesAvailable = <span>true</span>;<br />  <span>int</span> counter=0;<br />  <span>while</span><!USB><span>available</span>()){ <span>//wait for CH376S to verify command</span><br />    <span>delay</span>(1);<br />    counter++;<br />    <span>if</span>(counter>timeOut){<br />      <span><b>Serial</b></span>.<span>print</span>(<span>"TimeOut waiting for response: Error while: "</span>);<br />      <span><b>Serial</b></span>.<span>println</span>(errorMsg);<br />      bytesAvailable = <span>false</span>;<br />      <span>break</span>;<br />    }<br />  }<br />  <span>delay</span>(1);<br />  <span>return</span>(bytesAvailable);<br />}<br /><br /><span>//getResponseFromUSB================================================================================</span><br /><span>//is used to get any error codes or messages from the CH376S module (in response to certain commands)</span><br /><span>byte</span> getResponseFromUSB(){<br />  <span>byte</span> response = <span>byte</span>(0x00);<br />  <span>if</span> (USB.<span>available</span>()){<br />    response = USB.<span>read</span>();<br />  }<br />  <span>return</span>(response);<br />}<br /><br /><br /><br /><span>//blinkLED==========================================================================================</span><br /><span>//Turn an LED on for 1 second</span><br /><span>void</span> blinkLED(){<br />  <span>digitalWrite</span>(LED, <span>HIGH</span>);<br />  <span>delay</span>(1000);<br />  <span>digitalWrite</span>(LED,<span>LOW</span>);<br />}<br /><br /></pre> </td> </tr> </table></div></p> <br /> <p> If you copy and paste this code directly into the Arduino IDE; you may get a warning like this when you compile the code:<br />   <br />    "Low memory available, stability problems may occur". <br />  <br /> I managed to run the sketch without any issues, however, I did experience problems with some of the methods when I had made further memory hungry modifications. If you do encounter problems, I would recommend that you eliminate any methods which you do not plan to use, and perhaps reduce the number of Serial.print statements throughout the code. However, please note that some of the methods will not work unless the module is in the correct state, so be careful which methods you delete. For example, I found that I could get some simple functionality without the "USBdiskMount()" method. However, I could not read/write data beyond a certain length without this method.<br />   <br /> Also please note, that some of the methods called within the reading and writing sequence do not need to be called every time. They can be called once in setup, while other methods within the sequence will need to be called every time. I grouped them all together for simplicity. </p> <!--separator --><img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <p><h4>Serial Commands</h4> Have a look at the following presentation for a summary of the Serial commands used in this tutorial: <br />   <br /> <div> </div> </p> </div><p> <div> <!-- Concluding Comments --> </div> <br />  <br />  <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> </p><p> <div> If you like this page, please do me a favour and show your appreciation : <br /> <br />  <br /> Visit my <a href="https://plus.google.com/u/0/b/107402020974762902161/107402020974762902161/posts">ArduinoBasics Google + page</a>.<br /> Follow me on Twitter by looking for <a href="https://twitter.com/ArduinoBasics">ScottC @ArduinoBasics</a>.<br /> I can also be found on <a href="https://www.pinterest.com/ArduinoBasics/">Pinterest</a> and <a href="https://instagram.com/arduinobasics">Instagram</a>. <br /> Have a look at my videos on my <a href="https://www.youtube.com/user/ScottCMe/videos">YouTube channel</a>.<br /> </div> </p> <br /> <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> <p> However, if you do not have a google profile... <br />Feel free to share this page with your friends in any way you see fit. </p>

CH376S USB Read/Write module

Have you ever wondered if there was a way to store and retrieve data from a USB stick with an Arduino UNO? Most people choose SD cards to store their project data, but you may be surprised there IS a way!
IC Station have a nice little module which allows you store and retrieve your Arduino (or other MCU) project data to a USB stick.
 
I am not too sure why USB storage is not widely used in Arduino projects? These modules are not expensive, they have been around for quite a while, and are relatively simple to use. You do not need any libraries to get them to work, however, I must say that documentation for this module is not that easy to find. This site and this document proved to be very useful in my endevour to get this module working, and I hope my tutorial below will help you get started and bridge some of the information gaps.
 
The "CH376S USB read/write module" has a CH376S chip onboard which does most of the hard work for you. All you have to do is send the module some commands from the Arduino and the CH376S chip will do the rest. You can communicate with the module in three different ways:

  • Parallel communication
  • SPI communication
  • and Serial (UART) communication.

This project will show you the connections and code for the Serial (UART) communication method only.


 

Parts Required:

Remove the Jumper

When the CH376S USB module arrives in it's package, it will have a jumper between the TXD pin and GND. You will need to remove this jumper to make the necessary connections between the Arduino UNO and the CH376S USB module.


 

Fritzing Sketch

Please note, that the Arduino Sketch makes use of the Arduino UNO's onboard LED on digital pin 13. The Fritzing sketch below shows an LED + 300 ohm resistor on a breadboard. This is optional. The LED is not a necessary component of CH376S module communication.

Also be aware that the CH376S USB module has an onboard LED just above the TXD and GND pins near the USB port. This LED will only turn on providing the CH376S module is in USB mode AND a USB device has been inserted into the USB port. Both conditions must be met before the module's onboard LED will illuminate. You will not see the LED turn on just by powering the board.
 
The wire diagram below is the correct setup for Serial communication between an Arduino UNO and the CH376S module. If you wish to use SPI or Parallel communication, you will need to refer to the datasheet.


 
 

Arduino Sketch


 
  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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505

/* ===============================================================
      Project: CH376S USB Read/Write Module testing ground
       Author: Scott C
      Created: 1st May 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This project will allow you to perform many of the functions available on the CH376S module.
               Checking connection to the module, putting the module into USB mode, resetting the module, 
               reading, writing, appending text to files on the USB stick. This is very useful alternative to
               SD card modules, plus it doesn't need any libraries.
================================================================== */

#include <SoftwareSerial.h>

byte computerByte; //used to store data coming from the computer
byte USB_Byte; //used to store data coming from the USB stick
int LED = 13; //the LED is connected to digital pin 13
int timeOut = 2000; //TimeOut is 2 seconds. This is the amount of time you wish to wait for a response from the CH376S module.
String wrData = "What is the meaning of life ?"; //We will write this data to a newly created file.
String wrData2 = "42"; //We will append this data to an already existing file.

SoftwareSerial USB(10, 11); // Digital pin 10 on Arduino (RX) connects to TXD on the CH376S module
                                                      // Digital pin 11 on Arduino (TX) connects to RXD on the CH376S module
                                                      // GND on Arduino to GND on CH376S module
                                                      // 5V on Arduino to 5V on CH376S module
//==============================================================================================================================================
void setup() {
  Serial.begin(9600); // Setup serial communication with the computer (using a baud rate of 9600 on serial monitor)
  USB.begin(9600); // Setup serial communication with the CH376S module (using the default baud rate of 9600)
  pinMode(LED,OUTPUT); // Define digital pin 13 as an OUTPUT pin - so that we can use it with an LED
  digitalWrite(LED,LOW); // Turn off the LED
}

//================================================================================================================================================
void loop() {
  if(Serial.available()){
    computerByte = Serial.read(); //read any incoming bytes from the Serial monitor, and store this byte in the variable called computerByte
    if(computerByte==49){ //1 //If you send the number 1 from the serial monitor, the arduino will read it as digital number 49. Google "ascii table" for more info.
      printCommandHeader("COMMAND1: CHECK CONNECTION");
      checkConnection(0x01);                           // Check for successful connection and communication with the CH376S module.
    } 
    if(computerByte==50){ //2
     printCommandHeader("COMMAND2: set_USB_Mode");
      set_USB_Mode(0x06);                              // Code used to enable read/write communication and monitoring of the USB stick
    }
    if(computerByte==51){ //3
      printCommandHeader("COMMAND3: resetALL");
      resetALL();                                      // Reset the USB device
    }
    if(computerByte==52){ //4
      printCommandHeader("COMMAND4: Create and Write to File : TEST4.TXT");
      writeFile("TEST4.TXT", wrData); // Create a file called TEST4.TXT and then Write the contents of wrData to this file
    }
    if(computerByte==53){ //5
      printCommandHeader("COMMAND5: Read File: TEST4.TXT");
      readFile("TEST4.TXT"); // Read the contents of this file on the USB disk, and display contents in the Serial Monitor
    }
    if(computerByte==54){ //6
      printCommandHeader("COMMAND6: Append data to file: TEST4.TXT");
      appendFile("TEST4.TXT", wrData2); // Append data to the end of the file.
    }
    if(computerByte==55){ //7
      printCommandHeader("COMMAND7: Delete File: TEST4.TXT");
      fileDelete("TEST4.TXT"); // Delete the file named TEST4.TXT
    }
    if(computerByte==56){ //8
      printCommandHeader("COMMAND8: Read File: TEST2.TXT");
      readFile("TEST2.TXT"); // Read the contents of the TEST2.TXT file on the USB disk, and display contents in the Serial Monitor
    }
    if(computerByte==57){ //9
      printCommandHeader("COMMAND9: Read File: TEST3.TXT");
      readFile("TEST3.TXT"); // Read the contents of the TEST3.TXT file on the USB disk, and display contents in the Serial Monitor
    }
  }
  
  if(USB.available()){ // This is here to capture any unexpected data transmitted by the CH376S module
    Serial.print("CH376S has just sent this code:");
    Serial.println(USB.read(), HEX);
  }
}

//END OF LOOP FUNCTION ========================================================================================================================================

//print Command header
void printCommandHeader(String header){
   Serial.println("======================");
   Serial.println("");
   Serial.println(header);
   Serial.println("----------------------");
}

//checkConnection==================================================================================
//This function is used to check for successful communication with the CH376S module. This is not dependant of the presence of a USB stick.
//Send any value between 0 to 255, and the CH376S module will return a number = 255 - value. 
void checkConnection(byte value){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x06);
  USB.write(value);
  
  if(waitForResponse("checking connection")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==(255-value)){
       Serial.println(">Connection to CH376S was successful.");
       blinkLED();                               //blink the LED for 1 second if the connection was successful
    } else {
      Serial.print(">Connection to CH376S - FAILED.");
    }
  }
}

//set_USB_Mode=====================================================================================
//Make sure that the USB is inserted when using 0x06 as the value in this specific code sequence
void set_USB_Mode (byte value){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x15);
  USB.write(value);
  
  delay(20);
  
  if(USB.available()){
    USB_Byte=USB.read();
    //Check to see if the command has been successfully transmitted and acknowledged.
    if(USB_Byte==0x51){ // If true - the CH376S has acknowledged the command.
        Serial.println("set_USB_Mode command acknowledged"); //The CH376S will now check and monitor the USB port
        USB_Byte = USB.read();
        
        //Check to see if the USB stick is connected or not.
        if(USB_Byte==0x15){ // If true - there is a USB stick connected
          Serial.println("USB is present");
          blinkLED();                                     // If the process was successful, then turn the LED on for 1 second
        } else {
          Serial.print("USB Not present. Error code:"); // If the USB is not connected - it should return an Error code = FFH
          Serial.print(USB_Byte, HEX);
          Serial.println("H");
        }
        
    } else {
        Serial.print("CH3765 error! Error code:");
        Serial.print(USB_Byte, HEX);
        Serial.println("H");
    }   
  }
  delay(20);
}

//resetALL=========================================================================================
//This will perform a hardware reset of the CH376S module - which usually takes about 35 msecs =====
void resetALL(){
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x05);
    Serial.println("The CH376S module has been reset !");
    delay(200);
}

//readFile=====================================================================================
//This will send a series of commands to read data from a specific file (defined by fileName)
void readFile(String fileName){
  resetALL();                     //Reset the module
  set_USB_Mode(0x06);             //Set to USB Mode
  diskConnectionStatus();         //Check that communication with the USB device is possible
  USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
  setFileName(fileName);          //Set File name
  fileOpen();                     //Open the file for reading
  int fs = getFileSize(); //Get the size of the file
  fileRead();                     //***** Send the command to read the file ***
  fileClose(0x00);                //Close the file
}

//writeFile========================================================================================
//is used to create a new file and then write data to that file. "fileName" is a variable used to hold the name of the file (e.g TEST.TXT). "data" should not be greater than 255 bytes long. 
void writeFile(String fileName, String data){
  resetALL();                     //Reset the module
  set_USB_Mode(0x06);             //Set to USB Mode
  diskConnectionStatus();         //Check that communication with the USB device is possible
  USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
  setFileName(fileName);          //Set File name
  if(fileCreate()){ //Try to create a new file. If file creation is successful
    fileWrite(data);              //write data to the file.
  } else {
    Serial.println("File could not be created, or it already exists");
  }
  fileClose(0x01);
}

//appendFile()====================================================================================
//is used to write data to the end of the file, without erasing the contents of the file.
void appendFile(String fileName, String data){
    resetALL();                     //Reset the module
    set_USB_Mode(0x06);             //Set to USB Mode
    diskConnectionStatus();         //Check that communication with the USB device is possible
    USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
    setFileName(fileName);          //Set File name
    fileOpen();                     //Open the file
    filePointer(false); //filePointer(false) is to set the pointer at the end of the file. filePointer(true) will set the pointer to the beginning.
    fileWrite(data);                //Write data to the end of the file
    fileClose(0x01);                //Close the file using 0x01 - which means to update the size of the file on close.
}
  
//setFileName======================================================================================
//This sets the name of the file to work with
void setFileName(String fileName){
  Serial.print("Setting filename to:");
  Serial.println(fileName);
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x2F);
  USB.write(0x2F); // Every filename must have this byte to indicate the start of the file name.
  USB.print(fileName); // "fileName" is a variable that holds the name of the file. eg. TEST.TXT
  USB.write((byte)0x00); // you need to cast as a byte - otherwise it will not compile. The null byte indicates the end of the file name.
  delay(20);
}

//diskConnectionStatus================================================================================
//Check the disk connection status
void diskConnectionStatus(){
  Serial.println("Checking USB disk connection status");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x30);

  if(waitForResponse("Connecting to USB disk")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful
       Serial.println(">Connection to USB OK");
    } else {
      Serial.print(">Connection to USB - FAILED.");
    }
  }
}

//USBdiskMount========================================================================================
//initialise the USB disk and check that it is ready - this process is required if you want to find the manufacturing information of the USB disk
void USBdiskMount(){
  Serial.println("Mounting USB disk");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x31);

  if(waitForResponse("mounting USB disk")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful
       Serial.println(">USB Mounted - OK");
    } else {
      Serial.print(">Failed to Mount USB disk.");
    }
  }
}

//fileOpen========================================================================================
//opens the file for reading or writing
void fileOpen(){
  Serial.println("Opening file.");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x32);
  if(waitForResponse("file Open")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful
       Serial.println(">File opened successfully.");
    } else {
      Serial.print(">Failed to open file.");
    }
  }
}

//setByteRead=====================================================================================
//This function is required if you want to read data from the file. 
boolean setByteRead(byte numBytes){
  boolean bytesToRead=false;
  int timeCounter = 0;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3A);
  USB.write((byte)numBytes); //tells the CH376S how many bytes to read at a time
  USB.write((byte)0x00);
  if(waitForResponse("setByteRead")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x1D){ //read the CH376S message. If equal to 0x1D, data is present, so return true. Will return 0x14 if no data is present.
      bytesToRead=true;
    }
  }
  return(bytesToRead);


//getFileSize()===================================================================================
//writes the file size to the serial Monitor.
int getFileSize(){
  int fileSize=0;
  Serial.println("Getting File Size");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x0C);
  USB.write(0x68);
  delay(100);
  Serial.print("FileSize =");
  if(USB.available()){
    fileSize = fileSize + USB.read();
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255);
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255*255);
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255*255*255);
  }     
  Serial.println(fileSize);
  delay(10);
  return(fileSize);
}


//fileRead========================================================================================
//read the contents of the file
void fileRead(){
  Serial.println("Reading file:");
  byte firstByte = 0x00; //Variable to hold the firstByte from every transmission. Can be used as a checkSum if required.
  byte numBytes = 0x40; //The maximum value is 0x40 = 64 bytes
 
  while(setByteRead(numBytes)){ //This tells the CH376S module how many bytes to read on the next reading step. In this example, we will read 0x10 bytes at a time. Returns true if there are bytes to read, false if there are no more bytes to read.
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x27); //Command to read ALL of the bytes (allocated by setByteRead(x))
    if(waitForResponse("reading data")){ //Wait for the CH376S module to return data. TimeOut will return false. If data is being transmitted, it will return true.
        firstByte=USB.read(); //Read the first byte
        while(USB.available()){
          Serial.write(USB.read()); //Send the data from the USB disk to the Serial monitor
          delay(1); //This delay is necessary for successful Serial transmission
        }
    }
    if<!continueRead()){><span>//prepares the module for further reading. If false, stop reading.</span><br />      <span>break</span>; <span>//You need the continueRead() method if the data to be read from the USB device is greater than numBytes.</span><br />    }<br />  }<br />  <span><b>Serial</b></span>.<span>println</span>();<br />  <span><b>Serial</b></span>.<span>println</span>(<span>"NO MORE DATA"</span>);<br />}<br /><br /><span>//fileWrite=======================================================================================</span><br /><span>//are the commands used to write to the file</span><br /><span>void</span> fileWrite(<span>String</span> data){<br />  <span><b>Serial</b></span>.<span>println</span>(<span>"Writing to file:"</span>);<br />  <span>byte</span> dataLength = (<span>byte</span>) data.<span>length</span>(); <span>// This variable holds the length of the data to be written (in bytes)</span><br />  <span><b>Serial</b></span>.<span>println</span>(data);<br />  <span><b>Serial</b></span>.<span>print</span>(<span>"Data Length:"</span>);<br />  <span><b>Serial</b></span>.<span>println</span>(dataLength);<br />  <span>delay</span>(100);<br />  <span>// This set of commands tells the CH376S module how many bytes to expect from the Arduino. (defined by the "dataLength" variable)</span><br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x3C);<br />  USB.<span>write</span>((<span>byte</span>) dataLength);<br />  USB.<span>write</span>((<span>byte</span>) 0x00);<br />  <span>if</span>(waitForResponse(<span>"setting data Length"</span>)){ <span>// Wait for an acknowledgement from the CH376S module before trying to send data to it</span><br />    <span>if</span>(getResponseFromUSB()==0x1E){ <span>// 0x1E indicates that the USB device is in write mode.</span><br />      USB.<span>write</span>(0x57);<br />      USB.<span>write</span>(0xAB);<br />      USB.<span>write</span>(0x2D);<br />      USB.<span>print</span>(data); <span>// write the data to the file</span><br />  <br />      <span>if</span>(waitForResponse(<span>"writing data to file"</span>)){ <span>// wait for an acknowledgement from the CH376S module</span><br />      }<br />      <span><b>Serial</b></span>.<span>print</span>(<span>"Write code (normally FF and 14): "</span>);<br />      <span><b>Serial</b></span>.<span>print</span>(USB.<span>read</span>(),<span>HEX</span>); <span>// code is normally 0xFF</span><br />      <span><b>Serial</b></span>.<span>print</span>(<span>","</span>);<br />      USB.<span>write</span>(0x57);<br />      USB.<span>write</span>(0xAB);<br />      USB.<span>write</span>(0x3D); <span>// This is used to update the file size. Not sure if this is necessary for successful writing.</span><br />      <span>if</span>(waitForResponse(<span>"updating file size"</span>)){ <span>// wait for an acknowledgement from the CH376S module</span><br />      }<br />      <span><b>Serial</b></span>.<span>println</span>(USB.<span>read</span>(),<span>HEX</span>); <span>//code is normally 0x14</span><br />    }<br />  }<br />}<br /><br /><span>//continueRead()==================================================================================</span><br /><span>//continue to read the file : I could not get this function to work as intended.</span><br /><span>boolean</span> continueRead(){<br />  <span>boolean</span> readAgain = <span>false</span>;<br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x3B);<br />  <span>if</span>(waitForResponse(<span>"continueRead"</span>)){ <span>//wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.</span><br />     <span>if</span>(getResponseFromUSB()==0x14){ <span>//CH376S will send 0x14 if this command was successful</span><br />       readAgain=<span>true</span>;<br />     }<br />  }<br />  <span>return</span>(readAgain);<br />} <br /><br /><span>//fileCreate()========================================================================================</span><br /><span>//the command sequence to create a file</span><br /><span>boolean</span> fileCreate(){<br />  <span>boolean</span> createdFile = <span>false</span>;<br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x34);<br />  <span>if</span>(waitForResponse(<span>"creating file"</span>)){ <span>//wait for a response from the CH376S. If file has been created successfully, it will return true.</span><br />     <span>if</span>(getResponseFromUSB()==0x14){ <span>//CH376S will send 0x14 if this command was successful</span><br />       createdFile=<span>true</span>;<br />     }<br />  }<br />  <span>return</span>(createdFile);<br />}<br /><br /><br /><span>//fileDelete()========================================================================================</span><br /><span>//the command sequence to delete a file</span><br /><span>void</span> fileDelete(<span>String</span> fileName){<br />  setFileName(fileName);<br />  <span>delay</span>(20);<br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x35);<br />  <span>if</span>(waitForResponse(<span>"deleting file"</span>)){ <span>//wait for a response from the CH376S. If file has been created successfully, it will return true.</span><br />     <span>if</span>(getResponseFromUSB()==0x14){ <span>//CH376S will send 0x14 if this command was successful</span><br />       <span><b>Serial</b></span>.<span>println</span>(<span>"Successfully deleted file"</span>);<br />     }<br />  }<br />}<br />  <br /><br /><span>//filePointer========================================================================================</span><br /><span>//is used to set the file pointer position. true for beginning of file, false for the end of the file.</span><br /><span>void</span> filePointer(<span>boolean</span> fileBeginning){<br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x39);<br />  <span>if</span>(fileBeginning){<br />    USB.<span>write</span>((<span>byte</span>)0x00); <span>//beginning of file</span><br />    USB.<span>write</span>((<span>byte</span>)0x00);<br />    USB.<span>write</span>((<span>byte</span>)0x00);<br />    USB.<span>write</span>((<span>byte</span>)0x00);<br />  } <span>else</span> {<br />    USB.<span>write</span>((<span>byte</span>)0xFF); <span>//end of file</span><br />    USB.<span>write</span>((<span>byte</span>)0xFF);<br />    USB.<span>write</span>((<span>byte</span>)0xFF);<br />    USB.<span>write</span>((<span>byte</span>)0xFF);<br />  }<br />  <span>if</span>(waitForResponse(<span>"setting file pointer"</span>)){ <span>//wait for a response from the CH376S. </span><br />     <span>if</span>(getResponseFromUSB()==0x14){ <span>//CH376S will send 0x14 if this command was successful</span><br />       <span><b>Serial</b></span>.<span>println</span>(<span>"Pointer successfully applied"</span>);<br />     }<br />  }<br />}<br /><br /><br /><span>//fileClose=======================================================================================</span><br /><span>//closes the file</span><br /><span>void</span> fileClose(<span>byte</span> closeCmd){<br />  <span><b>Serial</b></span>.<span>println</span>(<span>"Closing file:"</span>);<br />  USB.<span>write</span>(0x57);<br />  USB.<span>write</span>(0xAB);<br />  USB.<span>write</span>(0x36);<br />  USB.<span>write</span>((<span>byte</span>)closeCmd); <span>// closeCmd = 0x00 = close without updating file Size, 0x01 = close and update file Size</span><br /><br />  <span>if</span>(waitForResponse(<span>"closing file"</span>)){ <span>// wait for a response from the CH376S. </span><br />     <span>byte</span> resp = getResponseFromUSB();<br />     <span>if</span>(resp==0x14){ <span>// CH376S will send 0x14 if this command was successful</span><br />       <span><b>Serial</b></span>.<span>println</span>(<span>">File closed successfully."</span>);<br />     } <span>else</span> {<br />       <span><b>Serial</b></span>.<span>print</span>(<span>">Failed to close file. Error code:"</span>);<br />       <span><b>Serial</b></span>.<span>println</span>(resp, <span>HEX</span>);<br />     }  <br />  }<br />}<br /><br /><span>//waitForResponse===================================================================================</span><br /><span>//is used to wait for a response from USB. Returns true when bytes become available, false if it times out.</span><br /><span>boolean</span> waitForResponse(<span>String</span> errorMsg){<br />  <span>boolean</span> bytesAvailable = <span>true</span>;<br />  <span>int</span> counter=0;<br />  <span>while</span><!USB><span>available</span>()){ <span>//wait for CH376S to verify command</span><br />    <span>delay</span>(1);<br />    counter++;<br />    <span>if</span>(counter>timeOut){<br />      <span><b>Serial</b></span>.<span>print</span>(<span>"TimeOut waiting for response: Error while: "</span>);<br />      <span><b>Serial</b></span>.<span>println</span>(errorMsg);<br />      bytesAvailable = <span>false</span>;<br />      <span>break</span>;<br />    }<br />  }<br />  <span>delay</span>(1);<br />  <span>return</span>(bytesAvailable);<br />}<br /><br /><span>//getResponseFromUSB================================================================================</span><br /><span>//is used to get any error codes or messages from the CH376S module (in response to certain commands)</span><br /><span>byte</span> getResponseFromUSB(){<br />  <span>byte</span> response = <span>byte</span>(0x00);<br />  <span>if</span> (USB.<span>available</span>()){<br />    response = USB.<span>read</span>();<br />  }<br />  <span>return</span>(response);<br />}<br /><br /><br /><br /><span>//blinkLED==========================================================================================</span><br /><span>//Turn an LED on for 1 second</span><br /><span>void</span> blinkLED(){<br />  <span>digitalWrite</span>(LED, <span>HIGH</span>);<br />  <span>delay</span>(1000);<br />  <span>digitalWrite</span>(LED,<span>LOW</span>);<br />}<br /><br /></pre> </td> </tr> </table></div></p> <br /> <p> If you copy and paste this code directly into the Arduino IDE; you may get a warning like this when you compile the code:<br />   <br />    "Low memory available, stability problems may occur". <br />  <br /> I managed to run the sketch without any issues, however, I did experience problems with some of the methods when I had made further memory hungry modifications. If you do encounter problems, I would recommend that you eliminate any methods which you do not plan to use, and perhaps reduce the number of Serial.print statements throughout the code. However, please note that some of the methods will not work unless the module is in the correct state, so be careful which methods you delete. For example, I found that I could get some simple functionality without the "USBdiskMount()" method. However, I could not read/write data beyond a certain length without this method.<br />   <br /> Also please note, that some of the methods called within the reading and writing sequence do not need to be called every time. They can be called once in setup, while other methods within the sequence will need to be called every time. I grouped them all together for simplicity. </p> <!--separator --><img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <p><h4>Serial Commands</h4> Have a look at the following presentation for a summary of the Serial commands used in this tutorial: <br />   <br /> <div> </div> </p> </div><p> <div> <!-- Concluding Comments --> </div> <br />  <br />  <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> </p><p> <div> If you like this page, please do me a favour and show your appreciation : <br /> <br />  <br /> Visit my <a href="https://plus.google.com/u/0/b/107402020974762902161/107402020974762902161/posts">ArduinoBasics Google + page</a>.<br /> Follow me on Twitter by looking for <a href="https://twitter.com/ArduinoBasics">ScottC @ArduinoBasics</a>.<br /> I can also be found on <a href="https://www.pinterest.com/ArduinoBasics/">Pinterest</a> and <a href="https://instagram.com/arduinobasics">Instagram</a>. <br /> Have a look at my videos on my <a href="https://www.youtube.com/user/ScottCMe/videos">YouTube channel</a>.<br /> </div> </p> <br /> <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> <p> However, if you do not have a google profile... <br />Feel free to share this page with your friends in any way you see fit. </p>

Arduino Heart Rate Monitor


Project Description


Heart Rate Monitors are very popular at the moment.
There is something very appealing about watching the pattern of your own heart beat. And once you see it, there is an unstoppable urge to try and control it. This simple project will allow you to visualize your heart beat, and will calculate your heart rate. Keep reading to learn how to create your very own heart rate monitor.


 

Parts Required:


Fritzing Sketch


 

 
 
 

Grove Base Shield to Module Connections


 


 

Arduino Sketch


 

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
/* =================================================================================================
      Project: Arduino Heart rate monitor
       Author: Scott C
      Created: 21st April 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This is a simple sketch that uses a Grove Ear-clip Heart Rate sensor attached to an Arduino UNO,
               which sends heart rate data to the computer via Serial communication. You can see the raw data
               using the Serial monitor on the Arduino IDE, however, this sketch was specifically
               designed to interface with the matching Processing sketch for a much nicer graphical display.
               NO LIBRARIES REQUIRED.
=================================================================================================== */

#define Heart 2                            //Attach the Grove Ear-clip sensor to digital pin 2.
#define LED 4                              //Attach an LED to digital pin 4

boolean beat = false; /* This "beat" variable is used to control the timing of the Serial communication
                                           so that data is only sent when there is a "change" in digital readings. */

//==SETUP==========================================================================================
void setup() {
  Serial.begin(9600); //Initialise serial communication
  pinMode(Heart, INPUT); //Set digital pin 2 (heart rate sensor pin) as an INPUT
  pinMode(LED, OUTPUT); //Set digital pin 4 (LED) to an OUTPUT
}


//==LOOP============================================================================================
void loop() {
  if(digitalRead(Heart)>0){ //The heart rate sensor will trigger HIGH when there is a heart beat
    if<!beat){><span>//Only send data when it first discovers a heart beat - otherwise it will send a high value multiple times</span><br />      beat=<span>true</span>; <span>//By changing the beat variable to true, it stops further transmissions of the high signal</span><br />      <span>digitalWrite</span>(LED, <span>HIGH</span>); <span>//Turn the LED on </span><br />      <span><b>Serial</b></span>.<span>println</span>(1023); <span>//Send the high value to the computer via Serial communication.</span><br />    }<br />  } <span>else</span> { <span>//If the reading is LOW, </span><br />    <span>if</span>(beat){ <span>//and if this has just changed from HIGH to LOW (first low reading)</span><br />      beat=<span>false</span>; <span>//change the beat variable to false (to stop multiple transmissions)</span><br />      <span>digitalWrite</span>(LED, <span>LOW</span>); <span>//Turn the LED off.</span><br />      <span><b>Serial</b></span>.<span>println</span>(0); <span>//then send a low value to the computer via Serial communication.</span><br />    }<br />  }<br />}</pre> </td> </tr> </table></div></p> <br />  <br />   <br />  <br />  <p> <h4><a href="https://processing.org/download/?processing">Processing Sketch</a></h4> <br />  <div> <table> <tr> <td> <pre> 1<br /> 2<br /> 3<br /> 4<br /> 5<br /> 6<br /> 7<br /> 8<br /> 9<br /> 10<br /> 11<br /> 12<br /> 13<br /> 14<br /> 15<br /> 16<br /> 17<br /> 18<br /> 19<br /> 20<br /> 21<br /> 22<br /> 23<br /> 24<br /> 25<br /> 26<br /> 27<br /> 28<br /> 29<br /> 30<br /> 31<br /> 32<br /> 33<br /> 34<br /> 35<br /> 36<br /> 37<br /> 38<br /> 39<br /> 40<br /> 41<br /> 42<br /> 43<br /> 44<br /> 45<br /> 46<br /> 47<br /> 48<br /> 49<br /> 50<br /> 51<br /> 52<br /> 53<br /> 54<br /> 55<br /> 56<br /> 57<br /> 58<br /> 59<br /> 60<br /> 61<br /> 62<br /> 63<br /> 64<br /> 65<br /> 66<br /> 67<br /> 68<br /> 69<br /> 70<br /> 71<br /> 72<br /> 73<br /> 74<br /> 75<br /> 76<br /> 77<br /> 78<br /> 79<br /> 80<br /> 81<br /> 82<br /> 83<br /> 84<br /> 85<br /> 86<br /> 87<br /> 88<br /> 89<br /> 90<br /> 91<br /> 92<br /> 93<br /> 94<br /> 95<br /> 96<br /> 97<br /> 98<br /> 99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br /></pre> </td> <td> <pre><br /><span>/* =================================================================================================</span><br /><span>       Project: Arduino Heart rate monitor</span><br /><span>        Author: Scott C</span><br /><span>       Created: 21st April 2015</span><br /><span>Processing IDE: 2.2.1</span><br /><span>       Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html</span><br /><span>   Description: A Grove Ear-clip heart rate sensor allows an Arduino UNO to sense your pulse.</span><br /><span>                The data obtained by the Arduino can then be sent to the computer via Serial communication</span><br /><span>                which is then displayed graphically using this Processing sketch.</span><br /><span>                </span><br /><span>=================================================================================================== */</span><br /><br /><span>import</span> processing.serial.*; <span>// Import the serial library to allow Serial communication with the Arduino</span><br /><br /><span>int</span> numOfRecs = 45; <span>// numOfRecs: The number of rectangles to display across the screen</span><br />Rectangle[] myRecs = <span>new</span> Rectangle[numOfRecs]; <span>// myRecs[]: Is the array of Rectangles. Rectangle is a custom class (programmed within this sketch)</span><br /><br />Serial myPort;                                         <br /><span>String</span> comPortString=<span>"0"</span>; <span>//comPortString: Is used to hold the string received from the Arduino</span><br /><span>float</span> arduinoValue = 0; <span>//arduinoValue: Is the float variable converted from comPortString</span><br /><span>boolean</span> beat = <span>false</span>; <span>// beat: Used to control for multiple high/low signals coming from the Arduino</span><br /><br /><span>int</span> totalTime = 0; <span>// totalTime: Is the variable used to identify the total time between beats</span><br /><span>int</span> lastTime = 0; <span>// lastTime: Is the variable used to remember when the last beat took place</span><br /><span>int</span> beatCounter = 0; <span>// beatCounter: Is used to keep track of the number of beats (in order to calculate the average BPM)</span><br /><span>int</span> totalBeats = 10; <span>// totalBeats: Tells the computer that we want to calculate the average BPM using 10 beats.</span><br /><span>int</span>[] BPM = <span>new</span> <span>int</span>[totalBeats]; <span>// BPM[]: Is the Beat Per Minute (BPM) array - to hold 10 BPM calculations</span><br /><span>int</span> sumBPM = 0; <span>// sumBPM: Is used to sum the BPM[] array values, and is then used to calculate the average BPM.</span><br /><span>int</span> avgBPM = 0; <span>// avgBPM: Is the variable used to hold the average BPM calculated value.</span><br /><br /><span>PFont</span> f, f2; <span>// f & f2 : Are font related variables. Used to store font properties. </span><br /><br /><br /><span>//==SETUP==============================================================================================</span><br /><span>void</span> <span><b>setup</b></span>(){<br />  <span>size</span>(<span>displayWidth</span>,<span>displayHeight</span>); <span>// Set the size of the display to match the monitor width and height</span><br />  <span>smooth</span>(); <span>// Draw all shapes with smooth edges.</span><br />  f = <span>createFont</span>(<span>"Arial"</span>,24); <span>// Initialise the "f" font variable - used for the "calibrating" text displayed at the beginning</span><br />  f2 = <span>createFont</span>(<span>"Arial"</span>,96); <span>// Initialise the "f2" font variable - used for the avgBPM display on screen</span><br />  <br />  <span>for</span>(<span>int</span> i=0; i<numOfRecs; i++){ <span>// Initialise the array of rectangles</span><br />    myRecs[i] = <span>new</span> Rectangle(i, numOfRecs);<br />  }<br />  <br />  <span>for</span>(<span>int</span> i=0; i<totalBeats; i++){ <span>// Initialise the BPM array</span><br />    BPM[i] = 0;<br />  }<br />  <br />  myPort = <span>new</span> Serial(<span>this</span>, Serial.<span>list</span>()[0], 9600); <span>// Start Serial communication with the Arduino using a baud rate of 9600</span><br />  myPort.bufferUntil(<span>'\n'</span>); <span>// Trigger a SerialEvent on new line</span><br />}<br /><br /><br /><span>//==DRAW==============================================================================================</span><br /><span>void</span> <span><b>draw</b></span>(){<br />  <span>background</span>(0); <span>// Set the background to BLACK (this clears the screen each time)</span><br />  drawRecs();                                           <span>// Method call to draw the rectangles on the screen</span><br />  drawBPM();                                            <span>// Method call to draw the avgBPM value to the top right of the screen</span><br />}<br /><br /><br /><span>//==drawRecs==========================================================================================</span><br /><span>void</span> drawRecs(){ <span>// This custom method will draw the rectangles on the screen </span><br />  myRecs[0].setSize(arduinoValue);                      <span>// Set the first rectangle to match arduinoValue; any positive value will start the animation.</span><br />  <span>for</span>(<span>int</span> i=numOfRecs-1; i>0; i--){ <span>// The loop counts backwards for coding efficiency - and is used to draw all of the rectangles to screen</span><br />    myRecs[i].setMult(i);                               <span>// setMulti creates the specific curve pattern. </span><br />    myRecs[i].setRed(avgBPM);                           <span>// The rectangles become more "Red" with higher avgBPM values</span><br />    myRecs[i].setSize(myRecs[i-1].getH());              <span>// The current rectangle size is determined by the height of the rectangle immediately to it's left</span><br />    <span>fill</span>(myRecs[i].getR(),myRecs[i].getG(), myRecs[i].getB()); <span>// Set the colour of this rectangle</span><br />    <span>rect</span>(myRecs[i].getX(), myRecs[i].getY(), myRecs[i].getW(), myRecs[i].getH()); <span>// Draw this rectangle</span><br />  }<br />}<br /><br /><br /><span>//==drawBPM===========================================================================================</span><br /><span>void</span> drawBPM(){ <span>// This custom method is used to calculate the avgBPM and draw it to screen.</span><br />  sumBPM = 0;                                           <span>// Reset the sumBPM variable</span><br />  avgBPM = 0;                                           <span>// Reset the avgBPM variable</span><br />  <span>boolean</span> calibrating = <span>false</span>; <span>// calibrating: this boolean variable is used to control when the avgBPM is displayed to screen</span><br />  <br />  <span>for</span>(<span>int</span> i=1; i<totalBeats; i++){<br />    sumBPM = sumBPM + BPM[i-1];                         <span>// Sum all of the BPM values in the BPM array.</span><br />    <span>if</span>(BPM[i-1]<1){ <span>// If any BPM values are equal to 0, then set the calibrating variable to true. </span><br />      calibrating = <span>true</span>; <span>// This will be used later to display "calibrating" on the screen.</span><br />    }<br />  }<br />  avgBPM = sumBPM/(totalBeats-1);                       <span>// Calculate the average BPM from all BPM values</span><br />                                                        <br />  <span>fill</span>(255); <span>// The text will be displayed as WHITE text</span><br />  <span>if</span>(calibrating){<br />    <span>textFont</span>(f);<br />    <span>text</span>(<span>"Calibrating"</span>, (4*<span>width</span>)/5, (<span>height</span>/5)); <span>// If the calibrating variable is TRUE, then display the word "Calibrating" on screen</span><br />    <span>fill</span>(0); <span>// Change the fill and stroke to black (0) so that other text is "hidden" while calibrating variable is TRUE</span><br />    <span>stroke</span>(0);<br />  } <span>else</span> {<br />    <span>textFont</span>(f2);<br />    <span>text</span>(avgBPM, (4*<span>width</span>)/5, (<span>height</span>/5)); <span>// If the calibrating variable is FALSE, then display the avgBPM variable on screen</span><br />    <span>stroke</span>(255); <span>// Change the stroke to white (255) to show the white line underlying the word BPM.</span><br />  }<br />  <br />   <span>textFont</span>(f);<br />   <span>text</span>(<span>"BPM"</span>, (82*<span>width</span>)/100, (<span>height</span>/11)); <span>// This will display the underlined word "BPM" when calibrating variable is FALSE.</span><br />   <span>line</span>((80*<span>width</span>)/100, (<span>height</span>/10),(88*<span>width</span>)/100, (<span>height</span>/10));<br />   <span>stroke</span>(0);<br />}<br /><br /><br /><span>//==serialEvent===========================================================================================</span><br /><span>void</span> serialEvent(Serial cPort){ <span>// This will be triggered every time a "new line" of data is received from the Arduino</span><br /> comPortString = cPort.readStringUntil(<span>'\n'</span>); <span>// Read this data into the comPortString variable.</span><br /> <span>if</span>(comPortString != <span>null</span>) { <span>// If the comPortString variable is not NULL then</span><br />   comPortString=<span>trim</span>(comPortString); <span>// trim any white space around the text.</span><br />   <span>int</span> i = <span>int</span>(<span>map</span>(<span>Integer</span>.<span>parseInt</span>(comPortString),1,1023,1,<span>height</span>)); <span>// convert the string to an integer, and map the value so that the rectangle will fit within the screen.</span><br />   arduinoValue = <span>float</span>(i); <span>// Convert the integer into a float value.</span><br />   <span>if</span> (!beat){<br />     <span>if</span>(arduinoValue>0){ <span>// When a beat is detected, the "trigger" method is called.</span><br />       trigger(<span>millis</span>()); <span>// millis() creates a timeStamp of when the beat occured.</span><br />       beat=<span>true</span>; <span>// The beat variable is changed to TRUE to register that a beat has been detected.</span><br />     }<br />   }<br />   <span>if</span> (arduinoValue<1){ <span>// When the Arduino value returns back to zero, we will need to change the beat status to FALSE.</span><br />     beat = <span>false</span>;<br />   }<br /> }<br />} <br /><br /><br /><span>//==trigger===========================================================================================</span><br /><span>void</span> trigger(<span>int</span> time){ <span>// This method is used to calculate the Beats per Minute (BPM) and to store the last 10 BPMs into the BPM[] array.</span><br />  totalTime = time - lastTime;                         <span>// totalTime = the current beat time minus the last time there was a beat.</span><br />  lastTime = time;                                     <span>// Set the lastTime variable to the current "time" for the next round of calculations.</span><br />  BPM[beatCounter] = 60000/totalTime;                  <span>// Calculate BPM from the totalTime. 60000 = 1 minute.</span><br />  beatCounter++;                                       <span>// Increment the beatCounter </span><br />  <span>if</span> (beatCounter>totalBeats-1){ <span>// Reset the beatCounter when the total number of BPMs have been stored into the BPM[] array.</span><br />    beatCounter=0;                                     <span>// This allows us to keep the last 10 BPM calculations at all times.</span><br />  }<br />}<br /><br /><br /><span>//==sketchFullScreen==========================================================================================</span><br /><span>boolean</span> sketchFullScreen() { <span>// This puts Processing into Full Screen Mode</span><br /> <span>return</span> <span>true</span>;<br />}<br /><br /><br /><span>//==Rectangle CLASS==================================================================================*********</span><br /><span>class</span> Rectangle{<br />  <span>float</span> xPos, defaultY, yPos, myWidth, myHeight, myMultiplier; <span>// Variables used for drawing rectangles</span><br />  <span>int</span> blueVal, greenVal, redVal; <span>// Variables used for the rectangle colour</span><br />  <br />  Rectangle(<span>int</span> recNum, <span>int</span> nRecs){ <span>// The rectangles are constructed using two variables. The total number of rectangles to be displayed, and the identification of this rectangle (recNum)</span><br />    myWidth = <span>displayWidth</span>/nRecs; <span>// The width of the rectangle is determined by the screen width and the total number of rectangles.</span><br />    xPos = recNum * myWidth;                                      <span>// The x Position of this rectangle is determined by the width of the rectangles (all same) and the rectangle identifier.</span><br />    defaultY=<span>displayHeight</span>/2; <span>// The default Y position of the rectangle is half way down the screen.</span><br />    yPos = defaultY;                                              <span>// yPos is used to adjust the position of the rectangle as the size changes.</span><br />    myHeight = 1;                                                 <span>// The height of the rectangle starts at 1 pixel</span><br />    myMultiplier = 1;                                             <span>// The myMultiplier variable will be used to create the funnel shaped path for the rectangles.</span><br />    redVal = 0;                                                   <span>// The red Value starts off being 0 - but changes with avgBPM. Higher avgBPM means higher redVal</span><br />    <br />    <span>if</span> (recNum>0){ <span>// The blue Value progressively increases with every rectangle (moving to the right of the screen)</span><br />      blueVal = (recNum*255)/nRecs;<br />    } <span>else</span> {<br />      blueVal = 0;<br />    }<br />    greenVal = 255-blueVal;                                       <span>// Initially, the green value is at the opposite end of the spectrum to the blue value.</span><br />  }<br />  <br />  <span>void</span> setSize(<span>float</span> newSize){ <span>// This is used to set the new size of each rectangle </span><br />    myHeight=newSize*myMultiplier;<br />    yPos=defaultY-(newSize/2);<br />  }<br />  <br />  <span>void</span> setMult(<span>int</span> i){ <span>// The multiplier is a function of COS, which means that it varies from 1 to 0.</span><br />    myMultiplier = <span>cos</span>(<span>radians</span>(i)); <span>// You can try other functions to experience different effects.</span><br />  }<br />  <br />  <span>void</span> setRed(<span>int</span> r){<br />    redVal = <span>int</span>(<span>constrain</span>(<span>map</span>(<span>float</span>(r), 60, 100, 0, 255),0,255)); <span>// setRed is used to change the redValue based on the "normal" value for resting BPM (60-100). </span><br />    greenVal = 255 - redVal;                                       <span>// When the avgBPM > 100, redVal will equal 255, and the greenVal will equal 0.</span><br />  }                                                                <span>// When the avgBPM < 60, redVal will equal 0, and greenVal will equal 255.</span><br />  <br />  <span>float</span> getX(){ <span>// get the x Position of the rectangle</span><br />    <span>return</span> xPos;<br />  }<br /> <br />  <span>float</span> getY(){ <span>// get the y Position of the rectangle</span><br />    <span>return</span> yPos;<br />  }<br />  <br />  <span>float</span> getW(){ <span>// get the width of the rectangle</span><br />    <span>return</span> myWidth;<br />  }<br />  <br />  <span>float</span> getH(){ <span>// get the height of the rectangle</span><br />    <span>return</span> myHeight;<br />  }<br />  <br />  <span>float</span> getM(){ <span>// get the Multiplier of the rectangle</span><br />    <span>return</span> myMultiplier;<br />  }<br />  <br />  <span>int</span> getB(){ <span>// get the "blue" component of the rectangle colour</span><br />    <span>return</span> blueVal;<br />  }<br />  <br />  <span>int</span> getR(){ <span>// get the "red" component of the rectangle colour</span><br />    <span>return</span> redVal;<br />  }<br />  <br />  <span>int</span> getG(){ <span>// get the "green" component of the rectangle colour</span><br />    <span>return</span> greenVal;<br />  }<br />}<br /><br /></pre> </td> </tr> </table></div></p> <br />  <br /> <p> <h4>Processing Code Discussion:</h4><br /> </p><p> The Rectangle class was created to store relevant information about each rectangle. By using a custom class, we were able to design our rectangles any way we wanted. These rectangles have properties and methods which allow us to easily control their position, size and colour. By adding some smart functionality to each rectangle, we were able to get the rectangle to automatically position and colour itself based on key values. </p> <p> The Serial library is used to allow communication with the Arduino. In this Processing sketch, the values obtained from the Arduino were converted to floats to allow easy calulations of the beats per minute (BPM). I am aware that I have over-engineered the serialEvent method somewhat, because the Arduino is only really sending two values. I didn't really need to convert the String. But I am happy with the end result, and it does the job I needed it to... </p> <div> <p> <div> <a href="http://4.bp.blogspot.com/-EVTCQ3vkgGc/VTnOarlOWSI/AAAAAAAABdc/MslEU5oirAY/s1600/Complete%2BWorkstation2.jpg"><img src="http://4.bp.blogspot.com/-EVTCQ3vkgGc/VTnOarlOWSI/AAAAAAAABdc/MslEU5oirAY/s1600/Complete%2BWorkstation2.jpg" /> </a> </div> </p> </div> </div><!--separator --><img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /><p> <div> This project is quite simple. I designed it so that you could omit the Processing code if you wanted to. In that scenario, you would only be left with a blinking LED that blinks in time with your pulse. The Processing code takes this project to the next level. It provides a nice animation and calculates the beats per minute (BPM). <br />   <br /> I hope you liked this tutorial. Please feel free to share it, comment or give it a plus one. If you didn't like it, I would still appreciate your constructive feedback. </div> <br />  <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> </p><p> <div> If you like this page, please do me a favour and show your appreciation : <br /> <br />  <br /> Visit my <a href="https://plus.google.com/u/0/b/107402020974762902161/107402020974762902161/posts">ArduinoBasics Google + page</a>.<br /> Follow me on Twitter by looking for <a href="https://twitter.com/ArduinoBasics">ScottC @ArduinoBasics</a>.<br /> I can also be found on <a href="https://www.pinterest.com/ArduinoBasics/">Pinterest</a> and <a href="https://instagram.com/arduinobasics">Instagram</a>. <br /> Have a look at my videos on my <a href="https://www.youtube.com/user/ScottCMe/videos">YouTube channel</a>.<br /> </div> </p> <br />  <br />  <p> <div> <a href="http://3.bp.blogspot.com/-x_TA-qhOCzM/VTnULXoWhQI/AAAAAAAABds/quh02BWGsec/s1600/Slide1.JPG"><img src="http://3.bp.blogspot.com/-x_TA-qhOCzM/VTnULXoWhQI/AAAAAAAABds/quh02BWGsec/s1600/Slide1.JPG" /></a></div><br /> </p> <br />  <br />  <br />  <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> <p> However, if you do not have a google profile... <br />Feel free to share this page with your friends in any way you see fit. </p>

Arduino Heart Rate Monitor


Project Description


Heart Rate Monitors are very popular at the moment.
There is something very appealing about watching the pattern of your own heart beat. And once you see it, there is an unstoppable urge to try and control it. This simple project will allow you to visualize your heart beat, and will calculate your heart rate. Keep reading to learn how to create your very own heart rate monitor.


 

Parts Required:


Fritzing Sketch


 

 
 
 

Grove Base Shield to Module Connections


 


 

Arduino Sketch


 

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
/* =================================================================================================
      Project: Arduino Heart rate monitor
       Author: Scott C
      Created: 21st April 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This is a simple sketch that uses a Grove Ear-clip Heart Rate sensor attached to an Arduino UNO,
               which sends heart rate data to the computer via Serial communication. You can see the raw data
               using the Serial monitor on the Arduino IDE, however, this sketch was specifically
               designed to interface with the matching Processing sketch for a much nicer graphical display.
               NO LIBRARIES REQUIRED.
=================================================================================================== */

#define Heart 2                            //Attach the Grove Ear-clip sensor to digital pin 2.
#define LED 4                              //Attach an LED to digital pin 4

boolean beat = false; /* This "beat" variable is used to control the timing of the Serial communication
                                           so that data is only sent when there is a "change" in digital readings. */

//==SETUP==========================================================================================
void setup() {
  Serial.begin(9600); //Initialise serial communication
  pinMode(Heart, INPUT); //Set digital pin 2 (heart rate sensor pin) as an INPUT
  pinMode(LED, OUTPUT); //Set digital pin 4 (LED) to an OUTPUT
}


//==LOOP============================================================================================
void loop() {
  if(digitalRead(Heart)>0){ //The heart rate sensor will trigger HIGH when there is a heart beat
    if<!beat){><span>//Only send data when it first discovers a heart beat - otherwise it will send a high value multiple times</span><br />      beat=<span>true</span>; <span>//By changing the beat variable to true, it stops further transmissions of the high signal</span><br />      <span>digitalWrite</span>(LED, <span>HIGH</span>); <span>//Turn the LED on </span><br />      <span><b>Serial</b></span>.<span>println</span>(1023); <span>//Send the high value to the computer via Serial communication.</span><br />    }<br />  } <span>else</span> { <span>//If the reading is LOW, </span><br />    <span>if</span>(beat){ <span>//and if this has just changed from HIGH to LOW (first low reading)</span><br />      beat=<span>false</span>; <span>//change the beat variable to false (to stop multiple transmissions)</span><br />      <span>digitalWrite</span>(LED, <span>LOW</span>); <span>//Turn the LED off.</span><br />      <span><b>Serial</b></span>.<span>println</span>(0); <span>//then send a low value to the computer via Serial communication.</span><br />    }<br />  }<br />}</pre> </td> </tr> </table></div></p> <br />  <br />   <br />  <br />  <p> <h4><a href="https://processing.org/download/?processing">Processing Sketch</a></h4> <br />  <div> <table> <tr> <td> <pre> 1<br /> 2<br /> 3<br /> 4<br /> 5<br /> 6<br /> 7<br /> 8<br /> 9<br /> 10<br /> 11<br /> 12<br /> 13<br /> 14<br /> 15<br /> 16<br /> 17<br /> 18<br /> 19<br /> 20<br /> 21<br /> 22<br /> 23<br /> 24<br /> 25<br /> 26<br /> 27<br /> 28<br /> 29<br /> 30<br /> 31<br /> 32<br /> 33<br /> 34<br /> 35<br /> 36<br /> 37<br /> 38<br /> 39<br /> 40<br /> 41<br /> 42<br /> 43<br /> 44<br /> 45<br /> 46<br /> 47<br /> 48<br /> 49<br /> 50<br /> 51<br /> 52<br /> 53<br /> 54<br /> 55<br /> 56<br /> 57<br /> 58<br /> 59<br /> 60<br /> 61<br /> 62<br /> 63<br /> 64<br /> 65<br /> 66<br /> 67<br /> 68<br /> 69<br /> 70<br /> 71<br /> 72<br /> 73<br /> 74<br /> 75<br /> 76<br /> 77<br /> 78<br /> 79<br /> 80<br /> 81<br /> 82<br /> 83<br /> 84<br /> 85<br /> 86<br /> 87<br /> 88<br /> 89<br /> 90<br /> 91<br /> 92<br /> 93<br /> 94<br /> 95<br /> 96<br /> 97<br /> 98<br /> 99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br /></pre> </td> <td> <pre><br /><span>/* =================================================================================================</span><br /><span>       Project: Arduino Heart rate monitor</span><br /><span>        Author: Scott C</span><br /><span>       Created: 21st April 2015</span><br /><span>Processing IDE: 2.2.1</span><br /><span>       Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html</span><br /><span>   Description: A Grove Ear-clip heart rate sensor allows an Arduino UNO to sense your pulse.</span><br /><span>                The data obtained by the Arduino can then be sent to the computer via Serial communication</span><br /><span>                which is then displayed graphically using this Processing sketch.</span><br /><span>                </span><br /><span>=================================================================================================== */</span><br /><br /><span>import</span> processing.serial.*; <span>// Import the serial library to allow Serial communication with the Arduino</span><br /><br /><span>int</span> numOfRecs = 45; <span>// numOfRecs: The number of rectangles to display across the screen</span><br />Rectangle[] myRecs = <span>new</span> Rectangle[numOfRecs]; <span>// myRecs[]: Is the array of Rectangles. Rectangle is a custom class (programmed within this sketch)</span><br /><br />Serial myPort;                                         <br /><span>String</span> comPortString=<span>"0"</span>; <span>//comPortString: Is used to hold the string received from the Arduino</span><br /><span>float</span> arduinoValue = 0; <span>//arduinoValue: Is the float variable converted from comPortString</span><br /><span>boolean</span> beat = <span>false</span>; <span>// beat: Used to control for multiple high/low signals coming from the Arduino</span><br /><br /><span>int</span> totalTime = 0; <span>// totalTime: Is the variable used to identify the total time between beats</span><br /><span>int</span> lastTime = 0; <span>// lastTime: Is the variable used to remember when the last beat took place</span><br /><span>int</span> beatCounter = 0; <span>// beatCounter: Is used to keep track of the number of beats (in order to calculate the average BPM)</span><br /><span>int</span> totalBeats = 10; <span>// totalBeats: Tells the computer that we want to calculate the average BPM using 10 beats.</span><br /><span>int</span>[] BPM = <span>new</span> <span>int</span>[totalBeats]; <span>// BPM[]: Is the Beat Per Minute (BPM) array - to hold 10 BPM calculations</span><br /><span>int</span> sumBPM = 0; <span>// sumBPM: Is used to sum the BPM[] array values, and is then used to calculate the average BPM.</span><br /><span>int</span> avgBPM = 0; <span>// avgBPM: Is the variable used to hold the average BPM calculated value.</span><br /><br /><span>PFont</span> f, f2; <span>// f & f2 : Are font related variables. Used to store font properties. </span><br /><br /><br /><span>//==SETUP==============================================================================================</span><br /><span>void</span> <span><b>setup</b></span>(){<br />  <span>size</span>(<span>displayWidth</span>,<span>displayHeight</span>); <span>// Set the size of the display to match the monitor width and height</span><br />  <span>smooth</span>(); <span>// Draw all shapes with smooth edges.</span><br />  f = <span>createFont</span>(<span>"Arial"</span>,24); <span>// Initialise the "f" font variable - used for the "calibrating" text displayed at the beginning</span><br />  f2 = <span>createFont</span>(<span>"Arial"</span>,96); <span>// Initialise the "f2" font variable - used for the avgBPM display on screen</span><br />  <br />  <span>for</span>(<span>int</span> i=0; i<numOfRecs; i++){ <span>// Initialise the array of rectangles</span><br />    myRecs[i] = <span>new</span> Rectangle(i, numOfRecs);<br />  }<br />  <br />  <span>for</span>(<span>int</span> i=0; i<totalBeats; i++){ <span>// Initialise the BPM array</span><br />    BPM[i] = 0;<br />  }<br />  <br />  myPort = <span>new</span> Serial(<span>this</span>, Serial.<span>list</span>()[0], 9600); <span>// Start Serial communication with the Arduino using a baud rate of 9600</span><br />  myPort.bufferUntil(<span>'\n'</span>); <span>// Trigger a SerialEvent on new line</span><br />}<br /><br /><br /><span>//==DRAW==============================================================================================</span><br /><span>void</span> <span><b>draw</b></span>(){<br />  <span>background</span>(0); <span>// Set the background to BLACK (this clears the screen each time)</span><br />  drawRecs();                                           <span>// Method call to draw the rectangles on the screen</span><br />  drawBPM();                                            <span>// Method call to draw the avgBPM value to the top right of the screen</span><br />}<br /><br /><br /><span>//==drawRecs==========================================================================================</span><br /><span>void</span> drawRecs(){ <span>// This custom method will draw the rectangles on the screen </span><br />  myRecs[0].setSize(arduinoValue);                      <span>// Set the first rectangle to match arduinoValue; any positive value will start the animation.</span><br />  <span>for</span>(<span>int</span> i=numOfRecs-1; i>0; i--){ <span>// The loop counts backwards for coding efficiency - and is used to draw all of the rectangles to screen</span><br />    myRecs[i].setMult(i);                               <span>// setMulti creates the specific curve pattern. </span><br />    myRecs[i].setRed(avgBPM);                           <span>// The rectangles become more "Red" with higher avgBPM values</span><br />    myRecs[i].setSize(myRecs[i-1].getH());              <span>// The current rectangle size is determined by the height of the rectangle immediately to it's left</span><br />    <span>fill</span>(myRecs[i].getR(),myRecs[i].getG(), myRecs[i].getB()); <span>// Set the colour of this rectangle</span><br />    <span>rect</span>(myRecs[i].getX(), myRecs[i].getY(), myRecs[i].getW(), myRecs[i].getH()); <span>// Draw this rectangle</span><br />  }<br />}<br /><br /><br /><span>//==drawBPM===========================================================================================</span><br /><span>void</span> drawBPM(){ <span>// This custom method is used to calculate the avgBPM and draw it to screen.</span><br />  sumBPM = 0;                                           <span>// Reset the sumBPM variable</span><br />  avgBPM = 0;                                           <span>// Reset the avgBPM variable</span><br />  <span>boolean</span> calibrating = <span>false</span>; <span>// calibrating: this boolean variable is used to control when the avgBPM is displayed to screen</span><br />  <br />  <span>for</span>(<span>int</span> i=1; i<totalBeats; i++){<br />    sumBPM = sumBPM + BPM[i-1];                         <span>// Sum all of the BPM values in the BPM array.</span><br />    <span>if</span>(BPM[i-1]<1){ <span>// If any BPM values are equal to 0, then set the calibrating variable to true. </span><br />      calibrating = <span>true</span>; <span>// This will be used later to display "calibrating" on the screen.</span><br />    }<br />  }<br />  avgBPM = sumBPM/(totalBeats-1);                       <span>// Calculate the average BPM from all BPM values</span><br />                                                        <br />  <span>fill</span>(255); <span>// The text will be displayed as WHITE text</span><br />  <span>if</span>(calibrating){<br />    <span>textFont</span>(f);<br />    <span>text</span>(<span>"Calibrating"</span>, (4*<span>width</span>)/5, (<span>height</span>/5)); <span>// If the calibrating variable is TRUE, then display the word "Calibrating" on screen</span><br />    <span>fill</span>(0); <span>// Change the fill and stroke to black (0) so that other text is "hidden" while calibrating variable is TRUE</span><br />    <span>stroke</span>(0);<br />  } <span>else</span> {<br />    <span>textFont</span>(f2);<br />    <span>text</span>(avgBPM, (4*<span>width</span>)/5, (<span>height</span>/5)); <span>// If the calibrating variable is FALSE, then display the avgBPM variable on screen</span><br />    <span>stroke</span>(255); <span>// Change the stroke to white (255) to show the white line underlying the word BPM.</span><br />  }<br />  <br />   <span>textFont</span>(f);<br />   <span>text</span>(<span>"BPM"</span>, (82*<span>width</span>)/100, (<span>height</span>/11)); <span>// This will display the underlined word "BPM" when calibrating variable is FALSE.</span><br />   <span>line</span>((80*<span>width</span>)/100, (<span>height</span>/10),(88*<span>width</span>)/100, (<span>height</span>/10));<br />   <span>stroke</span>(0);<br />}<br /><br /><br /><span>//==serialEvent===========================================================================================</span><br /><span>void</span> serialEvent(Serial cPort){ <span>// This will be triggered every time a "new line" of data is received from the Arduino</span><br /> comPortString = cPort.readStringUntil(<span>'\n'</span>); <span>// Read this data into the comPortString variable.</span><br /> <span>if</span>(comPortString != <span>null</span>) { <span>// If the comPortString variable is not NULL then</span><br />   comPortString=<span>trim</span>(comPortString); <span>// trim any white space around the text.</span><br />   <span>int</span> i = <span>int</span>(<span>map</span>(<span>Integer</span>.<span>parseInt</span>(comPortString),1,1023,1,<span>height</span>)); <span>// convert the string to an integer, and map the value so that the rectangle will fit within the screen.</span><br />   arduinoValue = <span>float</span>(i); <span>// Convert the integer into a float value.</span><br />   <span>if</span> (!beat){<br />     <span>if</span>(arduinoValue>0){ <span>// When a beat is detected, the "trigger" method is called.</span><br />       trigger(<span>millis</span>()); <span>// millis() creates a timeStamp of when the beat occured.</span><br />       beat=<span>true</span>; <span>// The beat variable is changed to TRUE to register that a beat has been detected.</span><br />     }<br />   }<br />   <span>if</span> (arduinoValue<1){ <span>// When the Arduino value returns back to zero, we will need to change the beat status to FALSE.</span><br />     beat = <span>false</span>;<br />   }<br /> }<br />} <br /><br /><br /><span>//==trigger===========================================================================================</span><br /><span>void</span> trigger(<span>int</span> time){ <span>// This method is used to calculate the Beats per Minute (BPM) and to store the last 10 BPMs into the BPM[] array.</span><br />  totalTime = time - lastTime;                         <span>// totalTime = the current beat time minus the last time there was a beat.</span><br />  lastTime = time;                                     <span>// Set the lastTime variable to the current "time" for the next round of calculations.</span><br />  BPM[beatCounter] = 60000/totalTime;                  <span>// Calculate BPM from the totalTime. 60000 = 1 minute.</span><br />  beatCounter++;                                       <span>// Increment the beatCounter </span><br />  <span>if</span> (beatCounter>totalBeats-1){ <span>// Reset the beatCounter when the total number of BPMs have been stored into the BPM[] array.</span><br />    beatCounter=0;                                     <span>// This allows us to keep the last 10 BPM calculations at all times.</span><br />  }<br />}<br /><br /><br /><span>//==sketchFullScreen==========================================================================================</span><br /><span>boolean</span> sketchFullScreen() { <span>// This puts Processing into Full Screen Mode</span><br /> <span>return</span> <span>true</span>;<br />}<br /><br /><br /><span>//==Rectangle CLASS==================================================================================*********</span><br /><span>class</span> Rectangle{<br />  <span>float</span> xPos, defaultY, yPos, myWidth, myHeight, myMultiplier; <span>// Variables used for drawing rectangles</span><br />  <span>int</span> blueVal, greenVal, redVal; <span>// Variables used for the rectangle colour</span><br />  <br />  Rectangle(<span>int</span> recNum, <span>int</span> nRecs){ <span>// The rectangles are constructed using two variables. The total number of rectangles to be displayed, and the identification of this rectangle (recNum)</span><br />    myWidth = <span>displayWidth</span>/nRecs; <span>// The width of the rectangle is determined by the screen width and the total number of rectangles.</span><br />    xPos = recNum * myWidth;                                      <span>// The x Position of this rectangle is determined by the width of the rectangles (all same) and the rectangle identifier.</span><br />    defaultY=<span>displayHeight</span>/2; <span>// The default Y position of the rectangle is half way down the screen.</span><br />    yPos = defaultY;                                              <span>// yPos is used to adjust the position of the rectangle as the size changes.</span><br />    myHeight = 1;                                                 <span>// The height of the rectangle starts at 1 pixel</span><br />    myMultiplier = 1;                                             <span>// The myMultiplier variable will be used to create the funnel shaped path for the rectangles.</span><br />    redVal = 0;                                                   <span>// The red Value starts off being 0 - but changes with avgBPM. Higher avgBPM means higher redVal</span><br />    <br />    <span>if</span> (recNum>0){ <span>// The blue Value progressively increases with every rectangle (moving to the right of the screen)</span><br />      blueVal = (recNum*255)/nRecs;<br />    } <span>else</span> {<br />      blueVal = 0;<br />    }<br />    greenVal = 255-blueVal;                                       <span>// Initially, the green value is at the opposite end of the spectrum to the blue value.</span><br />  }<br />  <br />  <span>void</span> setSize(<span>float</span> newSize){ <span>// This is used to set the new size of each rectangle </span><br />    myHeight=newSize*myMultiplier;<br />    yPos=defaultY-(newSize/2);<br />  }<br />  <br />  <span>void</span> setMult(<span>int</span> i){ <span>// The multiplier is a function of COS, which means that it varies from 1 to 0.</span><br />    myMultiplier = <span>cos</span>(<span>radians</span>(i)); <span>// You can try other functions to experience different effects.</span><br />  }<br />  <br />  <span>void</span> setRed(<span>int</span> r){<br />    redVal = <span>int</span>(<span>constrain</span>(<span>map</span>(<span>float</span>(r), 60, 100, 0, 255),0,255)); <span>// setRed is used to change the redValue based on the "normal" value for resting BPM (60-100). </span><br />    greenVal = 255 - redVal;                                       <span>// When the avgBPM > 100, redVal will equal 255, and the greenVal will equal 0.</span><br />  }                                                                <span>// When the avgBPM < 60, redVal will equal 0, and greenVal will equal 255.</span><br />  <br />  <span>float</span> getX(){ <span>// get the x Position of the rectangle</span><br />    <span>return</span> xPos;<br />  }<br /> <br />  <span>float</span> getY(){ <span>// get the y Position of the rectangle</span><br />    <span>return</span> yPos;<br />  }<br />  <br />  <span>float</span> getW(){ <span>// get the width of the rectangle</span><br />    <span>return</span> myWidth;<br />  }<br />  <br />  <span>float</span> getH(){ <span>// get the height of the rectangle</span><br />    <span>return</span> myHeight;<br />  }<br />  <br />  <span>float</span> getM(){ <span>// get the Multiplier of the rectangle</span><br />    <span>return</span> myMultiplier;<br />  }<br />  <br />  <span>int</span> getB(){ <span>// get the "blue" component of the rectangle colour</span><br />    <span>return</span> blueVal;<br />  }<br />  <br />  <span>int</span> getR(){ <span>// get the "red" component of the rectangle colour</span><br />    <span>return</span> redVal;<br />  }<br />  <br />  <span>int</span> getG(){ <span>// get the "green" component of the rectangle colour</span><br />    <span>return</span> greenVal;<br />  }<br />}<br /><br /></pre> </td> </tr> </table></div></p> <br />  <br /> <p> <h4>Processing Code Discussion:</h4><br /> </p><p> The Rectangle class was created to store relevant information about each rectangle. By using a custom class, we were able to design our rectangles any way we wanted. These rectangles have properties and methods which allow us to easily control their position, size and colour. By adding some smart functionality to each rectangle, we were able to get the rectangle to automatically position and colour itself based on key values. </p> <p> The Serial library is used to allow communication with the Arduino. In this Processing sketch, the values obtained from the Arduino were converted to floats to allow easy calulations of the beats per minute (BPM). I am aware that I have over-engineered the serialEvent method somewhat, because the Arduino is only really sending two values. I didn't really need to convert the String. But I am happy with the end result, and it does the job I needed it to... </p> <div> <p> <div> <a href="http://4.bp.blogspot.com/-EVTCQ3vkgGc/VTnOarlOWSI/AAAAAAAABdc/MslEU5oirAY/s1600/Complete%2BWorkstation2.jpg"><img src="http://4.bp.blogspot.com/-EVTCQ3vkgGc/VTnOarlOWSI/AAAAAAAABdc/MslEU5oirAY/s1600/Complete%2BWorkstation2.jpg" /> </a> </div> </p> </div> </div><!--separator --><img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /><p> <div> This project is quite simple. I designed it so that you could omit the Processing code if you wanted to. In that scenario, you would only be left with a blinking LED that blinks in time with your pulse. The Processing code takes this project to the next level. It provides a nice animation and calculates the beats per minute (BPM). <br />   <br /> I hope you liked this tutorial. Please feel free to share it, comment or give it a plus one. If you didn't like it, I would still appreciate your constructive feedback. </div> <br />  <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> </p><p> <div> If you like this page, please do me a favour and show your appreciation : <br /> <br />  <br /> Visit my <a href="https://plus.google.com/u/0/b/107402020974762902161/107402020974762902161/posts">ArduinoBasics Google + page</a>.<br /> Follow me on Twitter by looking for <a href="https://twitter.com/ArduinoBasics">ScottC @ArduinoBasics</a>.<br /> I can also be found on <a href="https://www.pinterest.com/ArduinoBasics/">Pinterest</a> and <a href="https://instagram.com/arduinobasics">Instagram</a>. <br /> Have a look at my videos on my <a href="https://www.youtube.com/user/ScottCMe/videos">YouTube channel</a>.<br /> </div> </p> <br />  <br />  <p> <div> <a href="http://3.bp.blogspot.com/-x_TA-qhOCzM/VTnULXoWhQI/AAAAAAAABds/quh02BWGsec/s1600/Slide1.JPG"><img src="http://3.bp.blogspot.com/-x_TA-qhOCzM/VTnULXoWhQI/AAAAAAAABds/quh02BWGsec/s1600/Slide1.JPG" /></a></div><br /> </p> <br />  <br />  <br />  <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> <p> However, if you do not have a google profile... <br />Feel free to share this page with your friends in any way you see fit. </p>

One Arduino


Arduino

is an "open source physical computing platform based on a simple microcontroller board". It is also a "development environment for writing software for the board" (Source: Arduino.cc). The first Arduino was born in 2005 in the classrooms of Interactive Design Institute in Ivrea, Italy. A nice presentation which shows a timeline of "how Arduino came to be" can be found here.
 
Arduino has been a very successful concept and creation. Ten years on, it has become one of, if not, the most popular prototyping and development platforms in the world. It has found it's way into the hearts of many makers, artists, programmers, developers and inventors. It has been used for millions of projects: from automatic garage door openers and tea makers to flamethrowers, robots, lighting displays and sound production. In fact, I cannot think of anything that the Arduino hasn't been used for.
 
But as you are pretty well aware, there has been a recent rift between the original founders of Arduino.
There is no need to go over the details here because it has been covered many times already on other sites. But if you would like to get up to speed on the Arduino Trademark dispute, I would suggest you read the following articles:
 
  1) Massimo Banzi's interview on Make
  2) Federico's letter to the makers on Arduino.org
  3) Court Transcripts from the United States Courts Archive
  4) What's in a name: The battle for the soul of Arduino - on ZD Net
  5) Arduino vs Arduino: Part II on Hackaday


 



My Letter to Arduino LLC/SRL


Why can't you get along? Why the deception? Why the greed?
 
Who does this benefit? It is a lose-lose battle, not only for Arduino SRL/LLC but for the whole Arduino community. We all love Arduino. We love the idea, the mission and the dream. We like to make things... We don't care who "the original" Arduino was or is. It doesn't matter. We just want...

We don't want different versions of the Arduino IDE (1.6.3 vs 1.7.0).
Some may say that you should redo the IDE altogether, but that is an entirely different topic :)

Can't you see what this is doing?
It is hard enough for tutorial makers and forum dwellers to keep up with the ever evolving IDE as it is. Making two different versions of the "same IDE" adds about 10 layers of confusion and complexity ! And for what ??? This will be confusing for the newbie, confusing for the helpers, the developers, for everyone.

I am guessing that my words will have no influence over your decisions. But I hope it does !
I plee with both of you to stand by your infinity symbol and work through your differences. Say what you need to say, but get back together and regroup - as ONE ARDUINO.

Your rift is likely to reverberate through to the Arduino Community aswell. Some people will side with Arduino LLC, some with Arduino SRL. And while some people will take a stance of indifference, there will be some that just look elsewhere.
 
I don't know if revealing the results of my poll will benefit the cause or add salt to the wounds, But I asked the question, and you probably want to know the answer. Who does the Arduino Community support right now?
 
The infographic below shows the results of the poll I posted a week ago:

This poll was posted on the Arduino Tutorial Google+ community page . I tried to be as unbiased as possible. People were only allowed to vote once (enforced by log in). Each voter would have been presented with the organisations in random order. And results were not published until now. The results above are accurate as of 14th April 2015 at 9am (in Australia)
 

As you can clearly see, Arduino LLC is highly favoured.
 
My personal preference is not captured by that infographic. I agree with the bald engineer, I would prefer #OneArduino. I would like the Arduino Community to stand united. I also want the Arduino founders to stand united! Please, work through your issues, stop being greedy and stop the deception. Arduino is more than a product. It is the heart of a greater movement.

This war is pointless - please STOP !

Kind Regards
Scott C



One Arduino


Arduino

is an "open source physical computing platform based on a simple microcontroller board". It is also a "development environment for writing software for the board" (Source: Arduino.cc). The first Arduino was born in 2005 in the classrooms of Interactive Design Institute in Ivrea, Italy. A nice presentation which shows a timeline of "how Arduino came to be" can be found here.
 
Arduino has been a very successful concept and creation. Ten years on, it has become one of, if not, the most popular prototyping and development platforms in the world. It has found it's way into the hearts of many makers, artists, programmers, developers and inventors. It has been used for millions of projects: from automatic garage door openers and tea makers to flamethrowers, robots, lighting displays and sound production. In fact, I cannot think of anything that the Arduino hasn't been used for.
 
But as you are pretty well aware, there has been a recent rift between the original founders of Arduino.
There is no need to go over the details here because it has been covered many times already on other sites. But if you would like to get up to speed on the Arduino Trademark dispute, I would suggest you read the following articles:
 
  1) Massimo Banzi's interview on Make
  2) Federico's letter to the makers on Arduino.org
  3) Court Transcripts from the United States Courts Archive
  4) What's in a name: The battle for the soul of Arduino - on ZD Net
  5) Arduino vs Arduino: Part II on Hackaday


 



My Letter to Arduino LLC/SRL


Why can't you get along? Why the deception? Why the greed?
 
Who does this benefit? It is a lose-lose battle, not only for Arduino SRL/LLC but for the whole Arduino community. We all love Arduino. We love the idea, the mission and the dream. We like to make things... We don't care who "the original" Arduino was or is. It doesn't matter. We just want...

We don't want different versions of the Arduino IDE (1.6.3 vs 1.7.0).
Some may say that you should redo the IDE altogether, but that is an entirely different topic :)

Can't you see what this is doing?
It is hard enough for tutorial makers and forum dwellers to keep up with the ever evolving IDE as it is. Making two different versions of the "same IDE" adds about 10 layers of confusion and complexity ! And for what ??? This will be confusing for the newbie, confusing for the helpers, the developers, for everyone.

I am guessing that my words will have no influence over your decisions. But I hope it does !
I plee with both of you to stand by your infinity symbol and work through your differences. Say what you need to say, but get back together and regroup - as ONE ARDUINO.

Your rift is likely to reverberate through to the Arduino Community aswell. Some people will side with Arduino LLC, some with Arduino SRL. And while some people will take a stance of indifference, there will be some that just look elsewhere.
 
I don't know if revealing the results of my poll will benefit the cause or add salt to the wounds, But I asked the question, and you probably want to know the answer. Who does the Arduino Community support right now?
 
The infographic below shows the results of the poll I posted a week ago:

This poll was posted on the Arduino Tutorial Google+ community page . I tried to be as unbiased as possible. People were only allowed to vote once (enforced by log in). Each voter would have been presented with the organisations in random order. And results were not published until now. The results above are accurate as of 14th April 2015 at 9am (in Australia)
 

As you can clearly see, Arduino LLC is highly favoured.
 
My personal preference is not captured by that infographic. I agree with the bald engineer, I would prefer #OneArduino. I would like the Arduino Community to stand united. I also want the Arduino founders to stand united! Please, work through your issues, stop being greedy and stop the deception. Arduino is more than a product. It is the heart of a greater movement.

This war is pointless - please STOP !

Kind Regards
Scott C



Arduino BeatBox

Create your very own Arduino BeatBox !

Home-made capacitive touch sensors are used to trigger the MP3 drum sounds stored on the Grove Serial MP3 player. I have used a number of tricks to get the most out of this module, and I was quite impressed on how well it did. Over 130 sounds were loaded onto the SDHC card. Most were drum sounds, but I added some farm animal noises to provide an extra element of surprise and entertainment. You can put any sounds you want on the module and play them back quickly. We'll put the Grove Serial MP3 module through it's paces and make it into a neat little BeatBox !!


Key learning objectives

  • How to make your own beatbox
  • How to make capacitive drum pad sensors without using resistors
  • How to speed up Arduino's Analog readings for better performance
  • How to generate random numbers on your Arduino


Parts Required:

Making the drum pads


 
 

Fritzing Sketch


 


 
 

Grove Connections


 


 
 

Grove Connections (without base shield)


 


 
 

Arduino Sketch


 
  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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

/* =================================================================================================
      Project: Arduino Beatbox
       Author: Scott C
      Created: 9th April 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This project uses home made capacitive sensors to trigger over 130 MP3 sounds
               on the Grove Serial MP3 player. 
               
               The ADCTouch library is used to eliminate the resistors from the Capacitive sensing circuit. 
               The code used for capacitive sensing was adapted from the ADCTouch library example sketches. 
               You can find the ADCTouch library and relevant example code here:
               http://playground.arduino.cc/Code/ADCTouch
               
               "Advanced Arduino ADC" is used to improve the analogRead() speed, and enhance the
               drum pad or capacitive sensor response time. The Advanced Arduino ADC code 
               was adapted from this site:
               http://www.microsmart.co.za/technical/2014/03/01/advanced-arduino-adc/
               
               
=================================================================================================== */
  #include <ADCTouch.h>
  #include <SoftwareSerial.h>
  
  
  //Global variables
  //===================================================================================================
  int potPin = A4; //Grove Sliding potentiometer is connected to Analog Pin 4
  int potVal = 0;
  byte mp3Vol = 0; //Variable used to control the volume of the MP3 player
  byte oldVol = 0;
  
  int buttonPin = 5; //Grove Button is connected to Digital Pin 5
  int buttonStatus = 0;
  
  byte SongNum[4] = {0x01,0x02,0x03,0x04}; //The first 4 songs will be assigned to the drum pads upon initialisation
  byte numOfSongs = 130; //Total number of MP3 songs/sounds loaded onto the SDHC card
  
  long randNumber; //Variable used to hold the random number - used to randomise the sounds.
  
  int ledState[4]; //Used to keep track of the status of all LEDs (on or off)
  int counter = 0;
  
  SoftwareSerial mp3(3, 4); // The Grove MP3 Player is connected to Arduino digital Pin 3 and 4 (Serial communication)
       
  int ref0, ref1, ref2, ref3; //reference values to remove offset
  int threshold = 100;
      
  // Define the ADC prescalers
  const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
  const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  
  
  
  //Setup()
  //===================================================================================================
  void setup(){
    //Initialise the Grove MP3 Module
    delay(2500); //Allow the MP3 module to power up
    mp3.begin(9600); //Begin Serial communication with the MP3 module
    setPlayMode(0x00);                        //0x00 = Single song - played once ie. not repeated. (default)
    
    //Define the Grove Button as an INPUT
    pinMode(buttonPin, INPUT);
    
    //Define the 4 LED Pins as OUTPUTs
    pinMode(8, OUTPUT); //Green LED
    pinMode(9, OUTPUT); //Blue LED
    pinMode(10, OUTPUT); //Red LED
    pinMode(11, OUTPUT); //Yellow LED
    
    //Make sure each LED is OFF, and store the state of the LED into a variable.
    for(int i=8;i<12;i++){
      digitalWrite(i, LOW);
      ledState[i-8]=0;
    } 
    
    //Double our clock speed from 125 kHz to 250 kHz
    ADCSRA &= ~PS_128;   // set up the ADC
    ADCSRA |= PS_64;    // set our own prescaler to 64
    
    //Create reference values to account for the capacitance of each pad.
    ref0 = ADCTouch.read(A0, 500);
    ref1 = ADCTouch.read(A1, 500); //Take 500 readings
    ref2 = ADCTouch.read(A2, 500);
    ref3 = ADCTouch.read(A3, 500);
    
     //This helps to randomise the drum pads.
     randomSeed(analogRead(0));
  }
  
  
  
  // Loop()
  //===================================================================================================
  void loop(){
     
    //Take a reading from the Grove Sliding Potentiometer, and set volume accordingly
    potVal = analogRead(potPin);
    mp3Vol = map(potVal, 0, 1023, 0,31); // Convert the potentometer reading (0 - 1023) to fit within the MP3 player's Volume range (0 - 31)
    if((mp3Vol>(oldVol+1))|(mp3Vol<(oldVol-1))){ // Only make a change to the Volume on the Grove MP3 player when the potentiometer value changes
      oldVol = mp3Vol;
      setVolume(mp3Vol);
      delay(10); // This delay is necessary with Serial communication to MP3 player
    }
    
    //Take a reading from the Pin attached to the Grove Button. If pressed, randomise the MP3 songs/sounds for each drum pad, and make the LEDs blink randomly.
    buttonStatus = digitalRead(buttonPin);
    if(buttonStatus==HIGH){
      SongNum[0]=randomSongChooser(1, 30);
      SongNum[1]=randomSongChooser(31, 60);
      SongNum[2]=randomSongChooser(61, 86);
      SongNum[3]=randomSongChooser(87, (int)numOfSongs);
      randomLEDBlink();
    }
    
    //Get the capacitive readings from each drum pad: 50 readings are taken from each pad. (default is 100)
    int value0 = ADCTouch.read(A0,50); // Green drum pad
    int value1 = ADCTouch.read(A1,50); // Blue drum pad
    int value2 = ADCTouch.read(A2,50); // Red drum pad
    int value3 = ADCTouch.read(A3,50); // Yellow drum pad
    
    //Remove the offset to account for the baseline capacitance of each pad.
    value0 -= ref0;       
    value1 -= ref1;
    value2 -= ref2;
    value3 -= ref3;
    
    
    //If any of the values exceed the designated threshold, then play the song/sound associated with that drum pad.
    //The associated LED will stay on for the whole time the drum pad is pressed, providing the value remains above the threshold.
    //The LED will turn off when the pad is not being touched or pressed.
    if(value0>threshold){
      digitalWrite(8, HIGH);
      playSong(00,SongNum[0]);
    }else{
      digitalWrite(8,LOW);
    }
    
    if(value1>threshold){
      digitalWrite(9, HIGH);
      playSong(00,SongNum[1]);
    }else{
      digitalWrite(9,LOW);
    }
    
    if(value2>threshold){
      digitalWrite(10, HIGH);
      playSong(00,SongNum[2]);
    }else{
      digitalWrite(10,LOW);
    }
    
    if(value3>threshold){
      digitalWrite(11, HIGH);
      playSong(00,SongNum[3]);
    }else{
      digitalWrite(11,LOW);
    }
  }
      
   
  // writeToMP3:
  // a generic function that simplifies each of the methods used to control the Grove MP3 Player
  //===================================================================================================
  void writeToMP3(byte MsgLEN, byte A, byte B, byte C, byte D, byte E, byte F){
    byte codeMsg[] = {MsgLEN, A,B,C,D,E,F};
    mp3.write(0x7E); //Start Code for every command = 0x7E
    for(byte i = 0; i<MsgLEN+1; i++){
      mp3.write(codeMsg[i]); //Send the rest of the command to the GROVE MP3 player
    }
  }
  
  
  //setPlayMode: defines how each song is to be played
  //===================================================================================================
  void setPlayMode(byte playMode){
    /* playMode options:
          0x00 = Single song - played only once ie. not repeated.  (default)
          0x01 = Single song - cycled ie. repeats over and over.
          0x02 = All songs - cycled 
          0x03 = play songs randomly                                           */
    writeToMP3(0x03, 0xA9, playMode, 0x7E, 0x00, 0x00, 0x00);  
  }
  
  
  //playSong: tells the Grove MP3 player to play the song/sound, and also which song/sound to play
  //===================================================================================================
  void playSong(byte songHbyte, byte songLbyte){
    writeToMP3(0x04, 0xA0, songHbyte, songLbyte, 0x7E, 0x00, 0x00);            
    delay(100);
  }
  
  
  //setVolume: changes the Grove MP3 player's volume to the designated level (0 to 31)
  //===================================================================================================
  void setVolume(byte Volume){
    byte tempVol = constrain(Volume, 0, 31); //Volume range = 00 (muted) to 31 (max volume)
    writeToMP3(0x03, 0xA7, tempVol, 0x7E, 0x00, 0x00, 0x00); 
  }
  
  
  //randomSongChooser: chooses a random song to play. The range of songs to choose from
  //is limited and defined by the startSong and endSong parameters.
  //===================================================================================================
  byte randomSongChooser(int startSong, int endSong){
    randNumber = random(startSong, endSong);
    return((byte) randNumber);
  }
  
  
  //randomLEDBlink: makes each LED blink randomly. The LEDs are attached to digital pins 8 to 12.
  //===================================================================================================
  void randomLEDBlink(){
   counter=8;
   for(int i=0; i<40; i++){
     int x = constrain((int)random(8,12),8,12);
     toggleLED(x);
     delay(random(50,100-i));
   }
     
    for(int i=8;i<12;i++){
      digitalWrite(i, HIGH);
    }
    delay(1000);
    for(int i=8;i<12;i++){
      digitalWrite(i, LOW);
      ledState[i-8]=0;
    }
  }
  
  
  //toggleLED: is used by the randomLEDBlink method to turn each LED on and off (randomly).
  //===================================================================================================
  void toggleLED(int pinNum){
    ledState[pinNum-8]= !ledState[pinNum-8];
    digitalWrite(pinNum, ledState[pinNum-8]);
  }


 

Arduino Code Discussion

You can see from the Arduino code above, that it uses the ADCTouch library. This library was chosen over the Capacitive Sensing Library to eliminate the need for a high value resistor which are commonly found in Capacitive Sensing projects).
 
To increase the speed of the Analog readings, I utilised one of the "Advanced Arduino ADC" techniques described by Guy van den Berg on this Microsmart website.
 
The readings are increased by modifying the Arduino's ADC clock speed from 125kHz to 250 kHz. I did notice an overall better response time with this modification. However, the Grove Serial MP3 player is limited by it's inability to play more than one song or sound at a time. This means that if you hit another drum pad while the current sound is playing, it will stop playing the current sound, and then play the selected sound. The speed at which it can perform this task was quite impressive. In fact it was much better than I thought it would be. But if you are looking for polyphonic playability, you will be dissapointed.
 
This Serial MP3 module makes use of a high quality MP3 audio chip known as the "WT5001". Therefore, you should be able to get some additional features and functionality from this document. Plus you may find some extra useful info from the Seeedstudio wiki. I have re-used some code from the Arduino Boombox tutorial... you will find extra Grove Serial MP3 functions on that page.
 
I will warn you... the Grove Serial MP3 player can play WAV files, however for some reason it would not play many of the sound files in this format. Once the sounds were converted to the MP3 format, I did not look back. So if you decide to take on this project, make sure your sound files are in MP3 format, you'll have a much better outcome.
 
I decided to introduce a random sound selection for each drum pad to extend the novelty of this instrument, which meant that I had to come up with a fancy way to illuminate the LEDs. I demonstrated some of my other LED sequences on my instagram account. I sometimes use instagram to show my work in progress.
 
Have a look at the video below to see this project in action, and putting the Grove Serial MP3 player through it's paces.
 

The Video


 


First there was the Arduino Boombox, and now we have the Arduino Beatbox..... who knows what will come next !
 
Whenever I create a new project, I like to improve my Arduino knowledge. Sometimes it takes me into some rather complicated topics. There is a lot I do not know about Arduino, but I am enjoying the journey. I hope you are too !! Please Google plus one this post if it helped you in any way. These tutorials are free, which means I survive on feedback and plus ones... all you have to do is just scroll a little bit more and click that button :)

 
 



If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.


 
 

 
 
 



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

Arduino BeatBox

Create your very own Arduino BeatBox !

Home-made capacitive touch sensors are used to trigger the MP3 drum sounds stored on the Grove Serial MP3 player. I have used a number of tricks to get the most out of this module, and I was quite impressed on how well it did. Over 130 sounds were loaded onto the SDHC card. Most were drum sounds, but I added some farm animal noises to provide an extra element of surprise and entertainment. You can put any sounds you want on the module and play them back quickly. We'll put the Grove Serial MP3 module through it's paces and make it into a neat little BeatBox !!


Key learning objectives

  • How to make your own beatbox
  • How to make capacitive drum pad sensors without using resistors
  • How to speed up Arduino's Analog readings for better performance
  • How to generate random numbers on your Arduino


Parts Required:

Making the drum pads


 
 

Fritzing Sketch


 


 
 

Grove Connections


 


 
 

Grove Connections (without base shield)


 


 
 

Arduino Sketch


 
  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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

/* =================================================================================================
      Project: Arduino Beatbox
       Author: Scott C
      Created: 9th April 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This project uses home made capacitive sensors to trigger over 130 MP3 sounds
               on the Grove Serial MP3 player. 
               
               The ADCTouch library is used to eliminate the resistors from the Capacitive sensing circuit. 
               The code used for capacitive sensing was adapted from the ADCTouch library example sketches. 
               You can find the ADCTouch library and relevant example code here:
               http://playground.arduino.cc/Code/ADCTouch
               
               "Advanced Arduino ADC" is used to improve the analogRead() speed, and enhance the
               drum pad or capacitive sensor response time. The Advanced Arduino ADC code 
               was adapted from this site:
               http://www.microsmart.co.za/technical/2014/03/01/advanced-arduino-adc/
               
               
=================================================================================================== */
  #include <ADCTouch.h>
  #include <SoftwareSerial.h>
  
  
  //Global variables
  //===================================================================================================
  int potPin = A4; //Grove Sliding potentiometer is connected to Analog Pin 4
  int potVal = 0;
  byte mp3Vol = 0; //Variable used to control the volume of the MP3 player
  byte oldVol = 0;
  
  int buttonPin = 5; //Grove Button is connected to Digital Pin 5
  int buttonStatus = 0;
  
  byte SongNum[4] = {0x01,0x02,0x03,0x04}; //The first 4 songs will be assigned to the drum pads upon initialisation
  byte numOfSongs = 130; //Total number of MP3 songs/sounds loaded onto the SDHC card
  
  long randNumber; //Variable used to hold the random number - used to randomise the sounds.
  
  int ledState[4]; //Used to keep track of the status of all LEDs (on or off)
  int counter = 0;
  
  SoftwareSerial mp3(3, 4); // The Grove MP3 Player is connected to Arduino digital Pin 3 and 4 (Serial communication)
       
  int ref0, ref1, ref2, ref3; //reference values to remove offset
  int threshold = 100;
      
  // Define the ADC prescalers
  const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
  const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  
  
  
  //Setup()
  //===================================================================================================
  void setup(){
    //Initialise the Grove MP3 Module
    delay(2500); //Allow the MP3 module to power up
    mp3.begin(9600); //Begin Serial communication with the MP3 module
    setPlayMode(0x00);                        //0x00 = Single song - played once ie. not repeated. (default)
    
    //Define the Grove Button as an INPUT
    pinMode(buttonPin, INPUT);
    
    //Define the 4 LED Pins as OUTPUTs
    pinMode(8, OUTPUT); //Green LED
    pinMode(9, OUTPUT); //Blue LED
    pinMode(10, OUTPUT); //Red LED
    pinMode(11, OUTPUT); //Yellow LED
    
    //Make sure each LED is OFF, and store the state of the LED into a variable.
    for(int i=8;i<12;i++){
      digitalWrite(i, LOW);
      ledState[i-8]=0;
    } 
    
    //Double our clock speed from 125 kHz to 250 kHz
    ADCSRA &= ~PS_128;   // set up the ADC
    ADCSRA |= PS_64;    // set our own prescaler to 64
    
    //Create reference values to account for the capacitance of each pad.
    ref0 = ADCTouch.read(A0, 500);
    ref1 = ADCTouch.read(A1, 500); //Take 500 readings
    ref2 = ADCTouch.read(A2, 500);
    ref3 = ADCTouch.read(A3, 500);
    
     //This helps to randomise the drum pads.
     randomSeed(analogRead(0));
  }
  
  
  
  // Loop()
  //===================================================================================================
  void loop(){
     
    //Take a reading from the Grove Sliding Potentiometer, and set volume accordingly
    potVal = analogRead(potPin);
    mp3Vol = map(potVal, 0, 1023, 0,31); // Convert the potentometer reading (0 - 1023) to fit within the MP3 player's Volume range (0 - 31)
    if((mp3Vol>(oldVol+1))|(mp3Vol<(oldVol-1))){ // Only make a change to the Volume on the Grove MP3 player when the potentiometer value changes
      oldVol = mp3Vol;
      setVolume(mp3Vol);
      delay(10); // This delay is necessary with Serial communication to MP3 player
    }
    
    //Take a reading from the Pin attached to the Grove Button. If pressed, randomise the MP3 songs/sounds for each drum pad, and make the LEDs blink randomly.
    buttonStatus = digitalRead(buttonPin);
    if(buttonStatus==HIGH){
      SongNum[0]=randomSongChooser(1, 30);
      SongNum[1]=randomSongChooser(31, 60);
      SongNum[2]=randomSongChooser(61, 86);
      SongNum[3]=randomSongChooser(87, (int)numOfSongs);
      randomLEDBlink();
    }
    
    //Get the capacitive readings from each drum pad: 50 readings are taken from each pad. (default is 100)
    int value0 = ADCTouch.read(A0,50); // Green drum pad
    int value1 = ADCTouch.read(A1,50); // Blue drum pad
    int value2 = ADCTouch.read(A2,50); // Red drum pad
    int value3 = ADCTouch.read(A3,50); // Yellow drum pad
    
    //Remove the offset to account for the baseline capacitance of each pad.
    value0 -= ref0;       
    value1 -= ref1;
    value2 -= ref2;
    value3 -= ref3;
    
    
    //If any of the values exceed the designated threshold, then play the song/sound associated with that drum pad.
    //The associated LED will stay on for the whole time the drum pad is pressed, providing the value remains above the threshold.
    //The LED will turn off when the pad is not being touched or pressed.
    if(value0>threshold){
      digitalWrite(8, HIGH);
      playSong(00,SongNum[0]);
    }else{
      digitalWrite(8,LOW);
    }
    
    if(value1>threshold){
      digitalWrite(9, HIGH);
      playSong(00,SongNum[1]);
    }else{
      digitalWrite(9,LOW);
    }
    
    if(value2>threshold){
      digitalWrite(10, HIGH);
      playSong(00,SongNum[2]);
    }else{
      digitalWrite(10,LOW);
    }
    
    if(value3>threshold){
      digitalWrite(11, HIGH);
      playSong(00,SongNum[3]);
    }else{
      digitalWrite(11,LOW);
    }
  }
      
   
  // writeToMP3:
  // a generic function that simplifies each of the methods used to control the Grove MP3 Player
  //===================================================================================================
  void writeToMP3(byte MsgLEN, byte A, byte B, byte C, byte D, byte E, byte F){
    byte codeMsg[] = {MsgLEN, A,B,C,D,E,F};
    mp3.write(0x7E); //Start Code for every command = 0x7E
    for(byte i = 0; i<MsgLEN+1; i++){
      mp3.write(codeMsg[i]); //Send the rest of the command to the GROVE MP3 player
    }
  }
  
  
  //setPlayMode: defines how each song is to be played
  //===================================================================================================
  void setPlayMode(byte playMode){
    /* playMode options:
          0x00 = Single song - played only once ie. not repeated.  (default)
          0x01 = Single song - cycled ie. repeats over and over.
          0x02 = All songs - cycled 
          0x03 = play songs randomly                                           */
    writeToMP3(0x03, 0xA9, playMode, 0x7E, 0x00, 0x00, 0x00);  
  }
  
  
  //playSong: tells the Grove MP3 player to play the song/sound, and also which song/sound to play
  //===================================================================================================
  void playSong(byte songHbyte, byte songLbyte){
    writeToMP3(0x04, 0xA0, songHbyte, songLbyte, 0x7E, 0x00, 0x00);            
    delay(100);
  }
  
  
  //setVolume: changes the Grove MP3 player's volume to the designated level (0 to 31)
  //===================================================================================================
  void setVolume(byte Volume){
    byte tempVol = constrain(Volume, 0, 31); //Volume range = 00 (muted) to 31 (max volume)
    writeToMP3(0x03, 0xA7, tempVol, 0x7E, 0x00, 0x00, 0x00); 
  }
  
  
  //randomSongChooser: chooses a random song to play. The range of songs to choose from
  //is limited and defined by the startSong and endSong parameters.
  //===================================================================================================
  byte randomSongChooser(int startSong, int endSong){
    randNumber = random(startSong, endSong);
    return((byte) randNumber);
  }
  
  
  //randomLEDBlink: makes each LED blink randomly. The LEDs are attached to digital pins 8 to 12.
  //===================================================================================================
  void randomLEDBlink(){
   counter=8;
   for(int i=0; i<40; i++){
     int x = constrain((int)random(8,12),8,12);
     toggleLED(x);
     delay(random(50,100-i));
   }
     
    for(int i=8;i<12;i++){
      digitalWrite(i, HIGH);
    }
    delay(1000);
    for(int i=8;i<12;i++){
      digitalWrite(i, LOW);
      ledState[i-8]=0;
    }
  }
  
  
  //toggleLED: is used by the randomLEDBlink method to turn each LED on and off (randomly).
  //===================================================================================================
  void toggleLED(int pinNum){
    ledState[pinNum-8]= !ledState[pinNum-8];
    digitalWrite(pinNum, ledState[pinNum-8]);
  }


 

Arduino Code Discussion

You can see from the Arduino code above, that it uses the ADCTouch library. This library was chosen over the Capacitive Sensing Library to eliminate the need for a high value resistor which are commonly found in Capacitive Sensing projects).
 
To increase the speed of the Analog readings, I utilised one of the "Advanced Arduino ADC" techniques described by Guy van den Berg on this Microsmart website.
 
The readings are increased by modifying the Arduino's ADC clock speed from 125kHz to 250 kHz. I did notice an overall better response time with this modification. However, the Grove Serial MP3 player is limited by it's inability to play more than one song or sound at a time. This means that if you hit another drum pad while the current sound is playing, it will stop playing the current sound, and then play the selected sound. The speed at which it can perform this task was quite impressive. In fact it was much better than I thought it would be. But if you are looking for polyphonic playability, you will be dissapointed.
 
This Serial MP3 module makes use of a high quality MP3 audio chip known as the "WT5001". Therefore, you should be able to get some additional features and functionality from this document. Plus you may find some extra useful info from the Seeedstudio wiki. I have re-used some code from the Arduino Boombox tutorial... you will find extra Grove Serial MP3 functions on that page.
 
I will warn you... the Grove Serial MP3 player can play WAV files, however for some reason it would not play many of the sound files in this format. Once the sounds were converted to the MP3 format, I did not look back. So if you decide to take on this project, make sure your sound files are in MP3 format, you'll have a much better outcome.
 
I decided to introduce a random sound selection for each drum pad to extend the novelty of this instrument, which meant that I had to come up with a fancy way to illuminate the LEDs. I demonstrated some of my other LED sequences on my instagram account. I sometimes use instagram to show my work in progress.
 
Have a look at the video below to see this project in action, and putting the Grove Serial MP3 player through it's paces.
 

The Video


 


First there was the Arduino Boombox, and now we have the Arduino Beatbox..... who knows what will come next !
 
Whenever I create a new project, I like to improve my Arduino knowledge. Sometimes it takes me into some rather complicated topics. There is a lot I do not know about Arduino, but I am enjoying the journey. I hope you are too !! Please Google plus one this post if it helped you in any way. These tutorials are free, which means I survive on feedback and plus ones... all you have to do is just scroll a little bit more and click that button :)

 
 



If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.


 
 

 
 
 



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

Arduino vs Arduino

What side do you support?

The form submission is anonymous.
I will post the results on Google+ around the 14th of April 2015.

Make your choice below:
Loading...



ScottC 08 Apr 11:31