I am trying to transmit a barcode protocol over audio with the piezo connected at pin 8.
Below is a big piece of code, sorry! Most of it is just a couple big arrays.
Anyway, using digitalWrite(8, HIGH); works well, but tone() does not. Any ideas? Are these huge arrays detrimental to Arduinos?
#include <String.h>
int charValue = 0;
char* input[] = {" ", "!"};
int length = 2;
int pause = 50;
int startValue;
int target[107][6]=
{
{2,1,2,2,2,2},
{2,2,2,1,2,2},
{2,2,2,2,2,1},
{1,2,1,2,2,3},
{1,2,1,3,2,2},
{1,3,1,2,2,2},
{1,2,2,2,1,3},
{1,2,2,3,1,2},
{1,3,2,2,1,2},
{2,2,1,2,1,3},
{2,2,1,3,1,2},
{2,3,1,2,1,2},
{1,1,2,2,3,2},
{1,2,2,1,3,2},
{1,2,2,2,3,1},
{1,1,3,2,2,2},
{1,2,3,1,2,2},
{1,2,3,2,2,1},
{2,2,3,2,1,1},
{2,2,1,1,3,2},
{2,2,1,2,3,1},
{2,1,3,2,1,2},
{2,2,3,1,1,2},
{3,1,2,1,3,1},
{3,1,1,2,2,2},
{3,2,1,1,2,2},
{3,2,1,2,2,1},
{3,1,2,2,1,2},
{3,2,2,1,1,2},
{3,2,2,2,1,1},
{2,1,2,1,2,3},
{2,1,2,3,2,1},
{2,3,2,1,2,1},
{1,1,1,3,2,3},
{1,3,1,1,2,3},
{1,3,1,3,2,1},
{1,1,2,3,1,3},
{1,3,2,1,1,3},
{1,3,2,3,1,1},
{2,1,1,3,1,3},
{2,3,1,1,1,3},
{2,3,1,3,1,1},
{1,1,2,1,3,3},
{1,1,2,3,3,1},
{1,3,2,1,3,1},
{1,1,3,1,2,3},
{1,1,3,3,2,1},
{1,3,3,1,2,1},
{3,1,3,1,2,1},
{2,1,1,3,3,1},
{2,3,1,1,3,1},
{2,1,3,1,1,3},
{2,1,3,3,1,1},
{2,1,3,1,3,1},
{3,1,1,1,2,3},
{3,1,1,3,2,1},
{3,3,1,1,2,1},
{3,1,2,1,1,3},
{3,1,2,3,1,1},
{3,3,2,1,1,1},
{3,1,4,1,1,1},
{2,2,1,4,1,1},
{4,3,1,1,1,1},
{1,1,1,2,2,4},
{1,1,1,4,2,2},
{1,2,1,1,2,4},
{1,2,1,4,2,1},
{1,4,1,1,2,2},
{1,4,1,2,2,1},
{1,1,2,2,1,4},
{1,1,2,4,1,2},
{1,2,2,1,1,4},
{1,2,2,4,1,1},
{1,4,2,1,1,2},
{1,4,2,2,1,1},
{2,4,1,2,1,1},
{2,2,1,1,1,4},
{4,1,3,1,1,1},
{2,4,1,1,1,2},
{1,3,4,1,1,1},
{1,1,1,2,4,2},
{1,2,1,1,4,2},
{1,2,1,2,4,1},
{1,1,4,2,1,2},
{1,2,4,1,1,2},
{1,2,4,2,1,1},
{4,1,1,2,1,2},
{4,2,1,1,1,2},
{4,2,1,2,1,1},
{2,1,2,1,4,1},
{2,1,4,1,2,1},
{4,1,2,1,2,1},
{1,1,1,1,4,3},
{1,1,1,3,4,1},
{1,3,1,1,4,1},
{1,1,4,1,1,3},
{1,1,4,3,1,1},
{4,1,1,1,1,3},
{4,1,1,3,1,1},
{1,1,3,1,4,1},
{1,1,4,1,3,1},
{3,1,1,1,4,1},
{4,1,1,1,3,1},
{2,1,1,4,1,2},
{2,1,1,2,1,4},
{2,1,1,2,3,2},
// {2,3,3,1,1,1,2},
{2,3,3,1,1,1}
// This is a hack. the stop sequence is the onlyone
// that has 7 bars but we check only for the first 6
// to make all arrays the same length
};
char* res_a[107] = {
" ",
"!",
"\"",
"#",
"$",
"%",
"&",
"'",
"(",
")",
"*",
"+",
",",
"-",
".",
"/",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
":",
";",
"<",
"=",
">",
"?",
"@",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"[",
"\\",
"]",
"^",
"_",
"NUL",
"SOH",
"STX",
"ETX",
"EOT",
"ENQ",
"ACK",
"BEL",
"BS",
"HT",
"LF",
"VT",
"FF",
"CR",
"SO",
"SI",
"DLE",
"DC1",
"DC2",
"DC3",
"DC4",
"NAK",
"SYN",
"ETB",
"CAN",
"EM",
"SUB",
"ESC",
"FS",
"GS",
"RS",
"US",
"FNC_3",
"FNC_2",
"SHIFT",
"CODE_C",
"CODE_B",
"FNC 4",
"FNC_1",
"Start_A",
"Start_B",
"Start_C",
"Stop"
};
void setup(){
}
void loop(){
// Create start sequence
for(int i; i < sizeof(res_a); i++){
if(res_a[i] == "Start_A"){
startValue = i;
}
}
delay(pause*11);
buzz(startValue);
//Encode and buzz information
for(int k = 0; k < length; k++){
for(int i = 0; i < sizeof(res_a); i++){
char* res = res_a[i];
char* inp = input[k];
if(res == inp){
// Serial.println(res);
charValue = i;
buzz(charValue);
}
else{
// Serial.println("Wrong Character!");
}
}
}
// checksum();
// stopbuzz();
// delay(pause);
}
int checksum(){
int CSValue = 100;
buzz(CSValue);
}
void stopbuzz(){
delay(pause*11);
}
int buzz(int charValue){
for(int i = 0; i<6; i++){
if((i % 2) == 0){
if (target[charValue][i] == 1){
tone(8, 100, pause);
noTone(8);
}
else if(target[charValue][i] == 2){
tone(8, 100, pause*2);
noTone(8);
}
else if(target[charValue][i] == 3){
tone(8, 100, pause*3);
noTone(8);
}
else if(target[charValue][i] == 4){
tone(8, 100, pause*4);
noTone(8);
}
}
else{
if (target[charValue][i] == 1){
tone(8, 200, pause);
noTone(8);
}
else if(target[charValue][i] == 2){
tone(8, 200, pause*2);
noTone(8);
}
else if(target[charValue][i] == 3){
tone(8, 200, pause*3);
noTone(8);
}
else if(target[charValue][i] == 4){
tone(8, 200, pause*4);
noTone(8);
}
}
}
}