Pages: [1]   Go Down
Author Topic: FM Modulated Barcode, Tone() Problem  (Read 716 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
#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);
       }
    }
  }   
}






Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well! It looks like analogWrite(); worked in a pinch. However, I'm curious if this works the same way. analogWrite() changes the timer settings to where millis(); and other functions will not work correctly. Is tone() the same way?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nevermind. I thought I had succeeded, but analogWrite() doesn't change frequency, just the percentage at which it works. Again, open to suggestions smiley
Logged

Montreal
Offline Offline
Edison Member
*
Karma: 23
Posts: 2486
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.mythic-beasts.com/~markt/ATmega-timers.html
Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Ugi was not declared in this scope
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?


Apologies if this is a daft question, but are you using a piezo transducer/speaker, or a piezo buzzer?

You say that digitalWrite(8, HIGH); works.  That should only make a click on a speaker/transducer.  If it's making a "tone" then it sounds like you have a piezo buzzer.  If you do then you will never be able to change the tone (well, not that I know of anyway - never say never) because it's generated by a circuit in the buzzer itself.

If you want to change the pitch then you need to use a piezo transducer/speaker and then "tone" should work perfectly well.

Of course I may be completely misunderstanding you here.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Its a piezo speaker. I am able to run the melody example that came with arduino
Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Ugi was not declared in this scope
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Then I am at a loss.  The code (to my inexperienced eye) looks fine and clearly the speaker works.

Only thing I wondered:
Code:
 delay(pause*11);
  buzz(startValue);

....
  
int buzz(int charValue){     ....}

You are defining the "buzz" function to return an integer but are calling it as if it was declared as "void".  I'm surprised that doesn't throw an error and perhaps you know that you can do this, but I would try:

void buzz(int charValue){     ....}

Just in case.
« Last Edit: December 13, 2011, 08:43:56 am by Dr_Ugi » Logged

Pages: [1]   Go Up
Jump to: