Go Down

Topic: Arduino Code Troubleshooting (Read 8578 times) previous topic - next topic

electricgirl

Hello,

I am using the lilypad arduino and am trying to compile a code and have the hardware work with it. The code compiles correctly and loads, although the hardware is not responding. I believe my accelerometer is incorrect.


Here is my code :


int led = 3;
int buzzer = 2;
int xaxis = A3;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);   
  pinMode(buzzer,OUTPUT);
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
 
 
 
  Serial.print(analogRead(xaxis));
  // print a tab between values:
  Serial.println("");

  delay(1000);
 
  if(xaxis>0)
  {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(10000);               // wait for a 10 seconds
  digitalWrite(led, LOW);
  delay(1000);
 
  }
}



-I have set the axis to be greater than 0 then to turn on the light, although no matter what the readings it will not do this. Any assistance or clarification on how to use the values for the accelerometer as an inequality?

Thank you!

groundFungus

xaxis is a pin number (A3).  You need to read the value of xaxis with analogRead and assign that to a variable
( value = analogRead(xaxis);  then if(value >=0)

electricgirl

I tried to do that as well although my LED won't light up :-/ Any other ideas? I used the serial monitor and can get readings from it, although I'm to sure why the LED won't light up

groundFungus

Try this code without the accel part, just the LED flashing by itself.  If it won't flash turn the LED around (maybe it is backwards).

Code: [Select]
int led = 3;

// the setup routine runs once when you press reset:
void setup() {
    // initialize the digital pin as an output.
    pinMode(led, OUTPUT);
    Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);               // wait for a 10 seconds
    digitalWrite(led, LOW);
    delay(1000);

}

electricgirl

Yes that does work correctly and the LED does flash.

groundFungus

#5
Sep 07, 2014, 06:34 pm Last Edit: Sep 07, 2014, 06:45 pm by groundfungus Reason: 1
Now try to read the accel by itself.  What is the raw data from the accel when sitting still and level and when turned 90 degrees from level?  Also, can you tell me how the LED is wired?

Code: [Select]
int xaxis = A3;

// the setup routine runs once when you press reset:
void setup() {              
 // initialize the digital pin as an output.
 Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
 int accel = analogRead(xaxis);
 Serial.print("Accel X ");
 Serial.println(accel);

 delay(1000);
 
}

electricgirl

So when this is levels, its reading is Accel 30, and when it is raised by 90 degrees its readings are roughly 19 consistently. The LED is hooked up to ground as well as LED 2

groundFungus

Try this.  This should light the LED when you tip the accel 90 degrees and turn the  LED off when level.  Note that I defined the pins as const byte.  That puts the pin variable in program memory to  save SRAM.  Not really necessary but good practice.

Code: [Select]

const byte xaxis = A3;
const byte led = 3;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  int accel = analogRead(xaxis);
  Serial.print("Accel X ");
  Serial.println(accel);
  if(accel < 25)
  {
      digitalWrite(led, HIGH);
  }
  else
  {
       digitalWrite(led, LOW);   
  }
   delay(1000); 
}

electricgirl

Yes! This worked! If I were to add in a tone I had made myself, do you think it can be easily added ? and if so would all the variables need to be const byte's?

groundFungus

#9
Sep 08, 2014, 04:31 am Last Edit: Sep 08, 2014, 04:38 am by groundfungus Reason: 1
Pin definitions are const byte because they will not change during the execution of the program.  Variables that will change can't be const.  So the to define a buzzer control PIN you would use const byte, but like the accel variable is not const because it will get a new value each time through loop(). It should be easy to add a tone.  Does your buzzer just take a digital high or low to sound?  Just add another if statement to turn on the buzzer and an else to turn the buzzer off.  If you have trouble, post your new code and we will try to get it going.

What acceleromter are you using?  You could probably get a better (wider span of output).

electricgirl

Code: [Select]
#include "pitches.h"
const byte xaxis = A3;
const byte speakerPin = 3;
const byte leds[]={2,10,11};


// Melody Notes
const byte melody[] = {
  NOTE_E7, NOTE_E7, 0, NOTE_E7,
  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,
  NOTE_G6, 0, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,

  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0,NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0,

  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,

  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0,NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0,
 
  NOTE_E7, NOTE_E7, 0, NOTE_E7,
  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,
  NOTE_G6, 0, 0, 0,
  };

// Each Note Duration
const byte noteDurations[] = {
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12
  };


// Play Melody
void play_tone(int note) {
  int noteDuration = 1000 / noteDurations[note];
  tone(7, melody[note], noteDuration);
  int pauseBetweenNotes = noteDuration * 1.30;
  delay(pauseBetweenNotes);
  noTone(7);
}

// Shut Down Every Leds
void reset_led() {
  digitalWrite(leds[0], LOW);
  digitalWrite(leds[1], LOW);
  digitalWrite(leds[2], LOW);

}



// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  Serial.begin(9600);
 
    for (int i = 0; i <= 4; i++) {
     pinMode(leds[i], OUTPUT);
  }
  pinMode(speakerPin, OUTPUT);

}


// the loop routine runs over and over again forever:
void loop() {
  int accel = analogRead(xaxis);
  Serial.print("Accel X ");
  Serial.println(accel);
  if(accel > 50)
  {
           int y = 0;
     // For Every Notes...
     for (int i = 0; i < 94; i++)
     {
       // Different of 0
       if (melody[i] != 0)
       {
         // Light up Led
         digitalWrite(leds[y], HIGH);
         y++;
         // Reset Count After Five Pins
         if (y > 4)
           y = 0;
       }
       play_tone(i);
       reset_led();
     }
  }
  else
  {
       
     digitalWrite(speakerPin, LOW);   
  }
   delay(1000); 
}










So heres is my code with a file that is referencing the notes I am trying to implement. I tried several different ways although the buzzer will not work. The LED will although. Any ideas? I put the "+" on the buzzer to pin 3 and the "-" to gnd.

groundFungus

Can you get the buzzer to play a single note by itself.  I mean, write a small program that just plays a note to confirm that your buzzer code works. 

electricgirl

Code: [Select]
int ledPin = 3;
int speakerPin = 2;
int xaxis = A3;
int Xax;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(ledPin, OUTPUT);   
  pinMode(speakerPin,OUTPUT);
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
 

  delay(1000);
  Xax = analogRead(xaxis);
 
    Serial.print(analogRead(Xax));
  // print a tab between values:
  Serial.println("");
  if(Xax>0)
  {scale();
  digitalWrite(ledPin, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(ledPin, LOW);

 
  }
}



void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)     // the sound producing function
{
          int x;
          long delayAmount = (long)(1000000/frequencyInHertz);
          long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
          for (x=0;x<loopTime;x++)
          {
              digitalWrite(speakerPin,HIGH);
              delayMicroseconds(delayAmount);
              digitalWrite(speakerPin,LOW);
              delayMicroseconds(delayAmount);
          }
}

void scale ()
{
          digitalWrite(ledPin,HIGH); //turn on the LED
          beep(speakerPin,2093,500); //C: play the note C (C7 from the chart linked to above) for 500ms
          beep(speakerPin,2349,500); //D
          beep(speakerPin,2637,500); //E
          beep(speakerPin,2793,500); //F
          beep(speakerPin,3136,500); //G
          beep(speakerPin,3520,500); //A
          beep(speakerPin,3951,500); //B
          beep(speakerPin,4186,500); //C
          digitalWrite(ledPin,LOW); //turn off the LED
}



I've tried this and my own code as well, also including some of the example tones in the arduino library and nothing has give me the correct outputs with the accel.. Any ideas that could help me ?

PaulS

Quote
Any ideas that could help me ?

Well, I've got this silly idea. Maybe, instead of chanting "That doesn't work", you could say what ACTUALLY happens when you run the code.
The art of getting good answers lies in asking good questions.

Go Up