Go Down

Topic: FM Modulated Barcode, Tone() Problem (Read 840 times) previous topic - next topic

alanhelton

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: [Select]
#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);
       }
    }
  }   
}







alanhelton

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?

alanhelton

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

Magician

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

Dr_Ugi


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.

alanhelton

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

Dr_Ugi

#6
Dec 13, 2011, 02:41 pm Last Edit: Dec 13, 2011, 02:43 pm by Dr_Ugi Reason: 1
Then I am at a loss.  The code (to my inexperienced eye) looks fine and clearly the speaker works.

Only thing I wondered:
Code: [Select]

 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.

Go Up