I am writing a program that reads multiple sensors on the Analog input pins and because I don't really understand how to initiate the switch/case parameters I am using if statements. The sketch works fine when only using 2 IF statements but when I get to the third nothing works. I didn't think there was a limit to the number of IF statements but am I wrong? Each IF statement is exactly the same so it's not syntax errors or anything like that. I am at my wits end trying to get this to work. Please help with any input possible. In the mean time I will keep working on it. Thank you in advance for any input.
There must be something wrong, post your code we can't tell you anything without it (and use the # button to format it properly).
Rob
GCC can handle 1000 if statements in 1 block... (i guess)
can u show us ur sketch?
do u use "if else if"?
i mean: maybe the 3rd if reverts the 2nd? then the 2nd reverts the 3rd, so that u cant c what u expect...
Here's the code. Each individual IF statement works but they won't work together.
#include <SD.h>
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
int Sensor1 = 0; //Variable for first sensor
int Sensor2 = 0; //Variable for second sensor
int Sensor3 = 0; //Variable for third sensor
int Sensor4 = 0; // for fourth sensor
int Sensor5 = 0; //Variable for fifth sensor
int MotorOn = 0; //Variable turning motor on
int MotorOff = 0; //Variable turning motor off
int Lights1 = 0; //Variable turning on LED cluster 1
int Lights2 = 0; //Variable turning on LED cluster 2
int blinky = 0;
void setup()
{
Serial.begin(9600); //Setup Serial
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
digitalWrite(A4,LOW);
digitalWrite(A5,LOW);
pinMode(A0, INPUT); //Assigns analog pins 0-4 as sensor inputs
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
pinMode(6, OUTPUT); //Assigns digital pin 6 as output
pinMode(7, OUTPUT); //Assigns analog pin 7 as output for motor
pinMode(8, OUTPUT); //Assigns digital pin 8 as output for LED cluster 1
pinMode(9, OUTPUT); //Assigns digital pin 9 as output for LED cluster 2
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
// while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
// card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
// while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
// while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
}
void sdErrorCheck(void)
{
if (!card.errorCode())
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
// while(1);
}
void playfile(char *name) {
// see if the wave object is currently doing something
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
}
// ok time to play! start playback
wave.play();
}
void ghost()
{
blinky = 0;
while(blinky < 40)
{
digitalWrite(8,HIGH);
delay(50); //set proper delay here for ghost to go to the end
digitalWrite(8,LOW);
delay(50);
blinky++;
}
}
void strobe1()
{
blinky = 0;
while(blinky<12)
{
digitalWrite(8,HIGH); //Blink LED cluster 1 and 2
digitalWrite(9,HIGH); //for first half of cat scream
delay(40);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(40);
blinky++;
}
}
void strobe2()
{
blinky = 0;
while(blinky < 16)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(80);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(80);
blinky++;
}
}
void strobe4()
{
blinky = 0;
while(blinky < 80)
{
digitalWrite(9,HIGH);
delay(50);
digitalWrite(9,LOW);
delay(50);
blinky++;
}
}
void strobe5()
{
blinky = 0;
while(blinky < 40)
{
digitalWrite(9,HIGH);
delay(40);
digitalWrite(9,LOW);
delay(30);
blinky++;
}
}
// Plays a full file from beginning to end with no pause.
//void playcomplete(char *name) {
// call our helper to find and play this name
// playfile(name);
// while (wave.isplaying) {
// do nothing while its playing
// }
// now its done playing
//}
void loop()
{
int Sensor1 = analogRead(A0); //Assigns values to the sensor readings
int Sensor2 = analogRead(A1);
int Sensor3 = analogRead(A2);
int Sensor4 = analogRead(A3);
int Sensor5 = analogRead(A4);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(7,LOW);
/* if(Sensor1 > 300)
{
playfile("cat.WAV");
while (wave.isplaying)
{
strobe1();
}
}
if(Sensor2 > 300)
{
playfile("suspense.WAV");
while(wave.isplaying)
{
strobe2();
}
}
*/
if(Sensor3 > 300)
{
digitalWrite(7,HIGH);
playfile("ghost.WAV");
while(wave.isplaying)
{
ghost();
}
digitalWrite(7,LOW);
}
/* if(Sensor4 > 300)
{
playfile("wlaugh2.WAV");
while(wave.isplaying)
{
strobe4();
}
}
if(Sensor5 > 300)
{
playfile("laugh3.WAV");
while(wave.isplaying)
{
strobe5();
}
}
*/ delay(10);
}
CODE tags please.
Highlight the code text and click the # button at the top of the editing area.
At present most of the loop() function is commented out, are they the if statements you are talking about?
Rob
Sorry about that. I have deleted unneeded segments and uncommented the IF statements. Each statement works by itself and in any variation of pairs but all 5 will not work together.
#include <SD.h>
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
int Sensor1 = 0; //Variable for first sensor
int Sensor2 = 0; //Variable for second sensor
int Sensor3 = 0; //Variable for third sensor
int Sensor4 = 0; // for fourth sensor
int Sensor5 = 0; //Variable for fifth sensor
int MotorOn = 0; //Variable turning motor on
int MotorOff = 0; //Variable turning motor off
int Lights1 = 0; //Variable turning on LED cluster 1
int Lights2 = 0; //Variable turning on LED cluster 2
int blinky = 0;
void setup()
{
Serial.begin(9600); //Setup Serial
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
digitalWrite(A4,LOW);
digitalWrite(A5,LOW);
pinMode(A0, INPUT); //Assigns analog pins 0-4 as sensor inputs
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
pinMode(6, OUTPUT); //Assigns digital pin 6 as output
pinMode(7, OUTPUT); //Assigns analog pin 7 as output for motor
pinMode(8, OUTPUT); //Assigns digital pin 8 as output for LED cluster 1
pinMode(9, OUTPUT); //Assigns digital pin 9 as output for LED cluster 2
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
// while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
// card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
// while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
// while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
}
void sdErrorCheck(void)
{
if (!card.errorCode())
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
// while(1);
}
void playfile(char *name) {
// see if the wave object is currently doing something
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
}
// ok time to play! start playback
wave.play();
}
void ghost()
{
blinky = 0;
while(blinky < 40)
{
digitalWrite(8,HIGH);
delay(50); //set proper delay here for ghost to go to the end
digitalWrite(8,LOW);
delay(50);
blinky++;
}
}
void strobe1()
{
blinky = 0;
while(blinky<12)
{
digitalWrite(8,HIGH); //Blink LED cluster 1 and 2
digitalWrite(9,HIGH); //for first half of cat scream
delay(40);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(40);
blinky++;
}
}
void strobe2()
{
blinky = 0;
while(blinky < 16)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(80);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(80);
blinky++;
}
}
void strobe4()
{
blinky = 0;
while(blinky < 80)
{
digitalWrite(9,HIGH);
delay(50);
digitalWrite(9,LOW);
delay(50);
blinky++;
}
}
void strobe5()
{
blinky = 0;
while(blinky < 40)
{
digitalWrite(9,HIGH);
delay(40);
digitalWrite(9,LOW);
delay(30);
blinky++;
}
}
void loop()
{
int Sensor1 = analogRead(A0); //Assigns values to the sensor readings
int Sensor2 = analogRead(A1);
int Sensor3 = analogRead(A2);
int Sensor4 = analogRead(A3);
int Sensor5 = analogRead(A4);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(7,LOW);
if(Sensor1 > 300)
{
playfile("cat.WAV");
while (wave.isplaying)
{
strobe1();
}
}
if(Sensor2 > 300)
{
playfile("suspense.WAV");
while(wave.isplaying)
{
strobe2();
}
}
if(Sensor3 > 300)
{
digitalWrite(7,HIGH);
playfile("ghost.WAV");
while(wave.isplaying)
{
ghost();
}
digitalWrite(7,LOW);
}
if(Sensor4 > 300)
{
playfile("wlaugh2.WAV");
while(wave.isplaying)
{
strobe4();
}
}
if(Sensor5 > 300)
{
playfile("laugh3.WAV");
while(wave.isplaying)
{
strobe5();
}
}
delay(10);
}
I didn't use ELSE IF because each IF statement is for a different sensor. The sensors are Photo Diodes with a laser pointed at them. The laser beam is walked through and then sends around 5VDC to the analog input pin. This is accomplished by putting the photo diode in a voltage divider feeding a BJT.
Dragonman502:
I am writing a program that reads multiple sensors on the Analog input pins and because I don't really understand how to initiate the switch/case parameters I am using if statements. The sketch works fine when only using 2 IF statements but when I get to the third nothing works. I didn't think there was a limit to the number of IF statements but am I wrong? Each IF statement is exactly the same so it's not syntax errors or anything like that. I am at my wits end trying to get this to work. Please help with any input possible. In the mean time I will keep working on it. Thank you in advance for any input.
Auto Format is your friend! Or it should be.
Are we to assume that you're talking about the IF statements in loop() or are you talking about playfile(char *name)? And could you be anymore general than "nothing works"?
Your problem isn't IF statements or not using CASE statements. Your problem is one of your code having some kind of bug. Very hard for anyone to help when you're being so general and not giving specifics.
A good way to diagnose your problem is to put statements like the following inside your IF statements (as the first command).
Serial.println("1");
With each command being a different number, letter, or something to describe each IF statement. Then, when you run your sketch you open the Serial Monitor so you can track where the code is and when it's failing.
In any case, it's not the fault of Arduino and using too many IF statements or needing to use CASE. The problem is your code is wrong. And for us to help specifically you should give DETAILS.
Tim
teckel I don't know how much more detail I can give. As I said. Each individual IF statement within the Loop() works perfectly fine and in any pair of two they work great but if I try to use three or more, nothing happens with ANY of the sensors. Nothing will trigger an output. I will try your advice and include a print statement at the beginning of each IF within the loop() and see what happens.
Have you tried taking out as much code as possible to make the problem still happen? For example, what happens if you replace the playfile() code with a single Serial.print statement? If the problem still exists, take some code out from somewhere else... this way, you have a lot less code, but you still have the problem which is then easier to track down (you just removed half the haystack).
Dragonman502:
teckel I don't know how much more detail I can give. As I said. Each individual IF statement within the Loop() works perfectly fine and in any pair of two they work great but if I try to use three or more, nothing happens with ANY of the sensors. Nothing will trigger an output. I will try your advice and include a print statement at the beginning of each IF within the loop() and see what happens.
Here's some more detail you could provide... What exactly are the sensors you are using and is it physically possible for more than one to be triggered at the same time?
Dragonman502:
teckel I don't know how much more detail I can give. As I said. Each individual IF statement within the Loop() works perfectly fine and in any pair of two they work great but if I try to use three or more, nothing happens with ANY of the sensors. Nothing will trigger an output. I will try your advice and include a print statement at the beginning of each IF within the loop() and see what happens.
First, you never said you were speaking of the IF statements in loop(). Up until now, you only mentioned the IF statements, which also exist outside loop(), which is why I asked. Please review what you wrote, you may have thought you said inside loop(), but you did not.
Secondly, all you've said is that "nothing works". Do you mean that you start the sketch and nothing AT ALL happens no matter what? Or, do you mean that the first thing works and the second doesn't? Or, maybe some things work, but it doesn't work prefect? Maybe try to give at least a sentence description of what happens from the time you start your sketch. Or, we are all to assume what you're experiencing, and maybe assuming different things or more importantly different from what is actually happening.
Keep in mind that we're trying to help. So, you should try to be as descriptive as possible. It's one thing to take your car in for service and say it doesn't work as they can see your car to diagnose it. But, we don't have that luxury here as we don't have your hardware in front of us to diagnose. So, it's very important to give details and be descriptive.
Tim
First, my apologies for seeming a bit rude in earlier posts. This has been turning out to be a frustrating project.
Second, I am posting some updated code below.
I am using five sensors that consist of a voltage divider with a photo resistor feeding a BJT and a red laser pointing on the photo resistor so that when someone walks through the beam the BJT turns on and sends 5VDC to the Arduino Uno digital input pin. I have the Wave Shield attached to the Arduino Uno and have various short wave files on the SD card. I also have various arrays of LED's on two output pins and a small DC motor attached to another output pin. I am using IF and ELSE IF statements now to read the input pins searching for one to go high from the 5VDC from the sensor. Each IF/ELSE IF statement has a different LED blink and a different audio file. When commenting out sensor 2-5, sensors 1 and 2 trigger outputs as they should but when I include all the sensors in the LOOP(), none of the sensors will trigger an output.
I have now discovered that if I only use three different audio files between the 5 conditional statements all 5 sensors trigger outputs. If I try only 4 audio files, none of the sensors trigger outputs.
I have tried inserting PRINT statements to print to the serial monitor for some debugging to see where it is getting stuck but I can't get that to work.
I haven't seen anything that limits the amount of audio files to be used. Even the WAVEHC example code for the six buttons there are six different audio files to be played.
Any input will be greatly appreciated.
In the mean time I will rewrite it to only use three audio files as this is my final project to graduate and is due this friday.
#include <SD.h>
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
void setup()
{
Serial.begin(9600); //Setup Serial
pinMode(14, INPUT); //Assigns analog pins 0-4 as sensor inputs
pinMode(15, INPUT);
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(19, INPUT);
pinMode(6, OUTPUT); //Assigns digital pin 6 as output
pinMode(7, OUTPUT); //Assigns analog pin 7 as output for motor
pinMode(8, OUTPUT); //Assigns digital pin 8 as output for LED cluster 1
pinMode(9, OUTPUT); //Assigns digital pin 9 as output for LED cluster 2
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
// while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
// card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
// while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
// while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
}
void sdErrorCheck(void)
{
if (!card.errorCode())
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
// while(1);
}
void playfile(char *name) {
// see if the wave object is currently doing something
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
}
// ok time to play! start playback
wave.play();
}
void ghost()
{
int blinky = 0;
while(blinky < 40)
{
digitalWrite(8,HIGH);
delay(50); //set proper delay here for ghost to go to the end
digitalWrite(8,LOW);
delay(50);
blinky++;
}
delay(1);
}
/*void strobe1()
{
int blinky = 0;
while(blinky<12)
{
digitalWrite(8,HIGH); //Blink LED cluster 1 and 2
digitalWrite(9,HIGH); //for first half of cat scream
delay(40);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(40);
blinky++;
}
}*/
void strobe2()
{
int blinky = 0;
while(blinky < 16)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(80);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(80);
blinky++;
}
}
void strobe4()
{
int blinky = 0;
while(blinky < 80)
{
digitalWrite(9,HIGH);
delay(50);
digitalWrite(9,LOW);
delay(50);
blinky++;
}
delay(1);
}
void strobe5()
{
int blinky = 0;
while(blinky < 40)
{
digitalWrite(9,HIGH);
delay(40);
digitalWrite(9,LOW);
delay(30);
blinky++;
}
delay(1);
}
// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
//call our helper to find and play this name
playfile(name);
while (wave.isplaying) {
// do nothing while its playing
}
// now its done playing
}
void loop()
{
if(digitalRead(14) == HIGH)
{
// blinky=0;
playfile("cat.WAV");
while (wave.isplaying)
{
//strobe1(); //try blinking light without function, you are already in a while loop
digitalWrite(8,HIGH); //Blink LED cluster 1 and 2
digitalWrite(9,HIGH); //for first half of cat scream
delay(40);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(40);
//blinky++;
}
// putstring_nl("finish sensor 1");
}
else if(digitalRead(15)==HIGH)
{
playfile("help.WAV");
while(wave.isplaying)
{
//strobe2();
}
}
else if(digitalRead(16)==HIGH)
{
// digitalWrite(7,HIGH);
playfile("ghost.WAV");
while(wave.isplaying)
{
//ghost();
}
// digitalWrite(7,LOW);
}
else if(digitalRead(17)==HIGH)
{
playfile("help.WAV");
while(wave.isplaying)
{
//strobe4();
}
}
else if(digitalRead(19)==HIGH)
{
playfile("ghost.WAV");
while(wave.isplaying)
{
//strobe5();
}
}
}
I have tried inserting PRINT statements to print to the serial monitor for some debugging to see where it is getting stuck but I can't get that to work.
You need to debug one thing at a time, forget the WAV files and blinking LEDs. Replace that code with a simple Serial.print() like
if(digitalRead(15)==HIGH) {
Serial.println ("help");
}
Then when that works start gradually putting stuff back in. When it breaks the last thing you added is (usually) the problem.
Rob
Graynomad:
I have tried inserting PRINT statements to print to the serial monitor for some debugging to see where it is getting stuck but I can't get that to work.
You need to debug one thing at a time, forget the WAV files and blinking LEDs. Replace that code with a simple Serial.print() like
if(digitalRead(15)==HIGH) {
Serial.println ("help");
}
Then when that works start gradually putting stuff back in. When it breaks the last thing you added is (usually) the problem. ______ Rob
+1
What I did with my project: I was making my robot to go around obstacles and end at the same spot that it would have ended up if it had not bumped into anything. I was using a simple Serial.print-ing, and after each time through the loop it'll print the current direction. The first time through it, it would go to "Foward" then "Right" once it made its first turn. Part of it was it recorded its direction. Problem: I used a switch loop and forgot to use "break;".
switch (direc)
{ // 8 - Forward, 4 - Left, 6 - Right, 2 - Back
case 8: // starts out 8, so changes to:
direc = 4; // 4.
case 6: // skips this:
direc = 8; // (skipped)
case 4: // It's 4, so now:
direc = 2; // 2.
case 2: // It's 2, so now:
direc = 6; // 6! Now it ends the switch loop and ends up with 6!!
default: ;
}
That's why it ended up w/ 6! That was my problem. The way I found out --> after almost each line I inserted a Serial.println of whatever just happened. Then I delayed it 3 seconds, so I could make sure it's right. Then I saw the error easily and figured out "I need to insert a 'break;'"!!
So basically, Serial debugging REALLY helped. I wouldn't have found the mistake without it. Serial print all variables discussed in the previous line, delay it enough time that you can check it's working, and Good Luck!!
Ps. I'll see if I can check the code.
I have no clue what you're thing is doing except finally playing some song or two. Since I don't have that library you're using, I needed to comment out ALL that library code.
When I opened Serial Moniter, this is what after like 15 sec showed up and starts repeading:
0
0
cat.WAV
suspense.WAV
ghost.WAV
wlaugh2.WAV
laugh3.WAV
This was the code, all commented out. Don't forget to uncomment and then test again:
/*
#include <SD.h>
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
*/
int Sensor1 = 0; //Variable for first sensor
int Sensor2 = 0; //Variable for second sensor
int Sensor3 = 0; //Variable for third sensor
int Sensor4 = 0; // for fourth sensor
int Sensor5 = 0; //Variable for fifth sensor
int MotorOn = 0; //Variable turning motor on
int MotorOff = 0; //Variable turning motor off
int Lights1 = 0; //Variable turning on LED cluster 1
int Lights2 = 0; //Variable turning on LED cluster 2
int blinky = 0;
void setup()
{
Serial.begin(9600); //Setup Serial
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
digitalWrite(A4,LOW);
digitalWrite(A5,LOW);
pinMode(A0, INPUT); //Assigns analog pins 0-4 as sensor inputs
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
pinMode(6, OUTPUT); //Assigns digital pin 6 as output
pinMode(7, OUTPUT); //Assigns analog pin 7 as output for motor
pinMode(8, OUTPUT); //Assigns digital pin 8 as output for LED cluster 1
pinMode(9, OUTPUT); //Assigns digital pin 9 as output for LED cluster 2
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
// if (!card.init()) { //play with 8 MHz spi (default faster!)
// putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
//sdErrorCheck();
// while(1); // then 'halt' - do nothing!
//}
// enable optimize read - some cards may timeout. Disable if you're having problems
// card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for(long part = 0; part < 5; part++) { // we have up to 5 slots to look in
Serial.println(part);
// if (vol.init(card, part)) {
break; // we found one, lets bail
// }
if (part == 5) { // if we ended up not finding one :(
// putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
// while(1); // then 'halt' - do nothing!
}
}
// Lets tell the user about what we found
// putstring("Using partition ");
Serial.println(part, DEC);
// putstring(", type is FAT");
// Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
// if (!root.openRoot(vol)) {
// putstring_nl("Can't open root dir!"); // Something went wrong,
// while(1); // then 'halt' - do nothing!
// }
// Whew! We got past the tough parts.
// putstring_nl("Ready!");
}
void loop()
{
int Sensor1 = analogRead(A0); //Assigns values to the sensor readings
int Sensor2 = analogRead(A1);
int Sensor3 = analogRead(A2);
int Sensor4 = analogRead(A3);
int Sensor5 = analogRead(A4);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(7,LOW);
if(Sensor1 > 300)
{
playfile("cat.WAV");
// while (wave.isplaying)
// {
strobe1();
// }
}
if(Sensor2 > 300)
{
playfile("suspense.WAV");
// while(wave.isplaying)
// {
strobe2();
// }
}
if(Sensor3 > 300)
{
digitalWrite(7,HIGH);
playfile("ghost.WAV");
// while(wave.isplaying)
// {
ghost();
// }
digitalWrite(7,LOW);
}
if(Sensor4 > 300)
{
playfile("wlaugh2.WAV");
// while(wave.isplaying)
// {
strobe4();
// }
}
if(Sensor5 > 300)
{
playfile("laugh3.WAV");
// while(wave.isplaying)
// {
strobe5();
// }
}
delay(10);
}
void sdErrorCheck()
{
// if (!card.errorCode())
// {
// putstring("\n\rSD I/O error: ");
// }
// Serial.print(card.errorCode(), HEX);
// putstring(", ");
// Serial.println(card.errorData(), HEX);
// while(1);
}
void playfile(char *name) {
// see if the wave object is currently doing something
// if (wave.isplaying) {// already playing something, so stop it!
// wave.stop(); // stop it
// }
// look in the root directory and open the file
// if (!f.open(root, name)) {
// putstring("Couldn't open file ");
Serial.println(name);
// }
// OK read the file and turn it into a wave object
// if (!wave.create(f)) {
// putstring_nl("Not a valid WAV");
// }
// ok time to play! start playback
// wave.play();
}
void ghost()
{
blinky = 0;
while(blinky < 40)
{
digitalWrite(8,HIGH);
delay(50); //set proper delay here for ghost to go to the end
digitalWrite(8,LOW);
delay(50);
blinky++;
}
}
void strobe1()
{
blinky = 0;
while(blinky<12)
{
digitalWrite(8,HIGH); //Blink LED cluster 1 and 2
digitalWrite(9,HIGH); //for first half of cat scream
delay(40);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(40);
blinky++;
}
}
void strobe2()
{
blinky = 0;
while(blinky < 16)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(80);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(80);
blinky++;
}
}
void strobe4()
{
blinky = 0;
while(blinky < 80)
{
digitalWrite(9,HIGH);
delay(50);
digitalWrite(9,LOW);
delay(50);
blinky++;
}
}
void strobe5()
{
blinky = 0;
while(blinky < 40)
{
digitalWrite(9,HIGH);
delay(40);
digitalWrite(9,LOW);
delay(30);
blinky++;
}
}
for some reason the Serial.println statements do not work. I have now discovered that only 3 wave files can be included in the code. I don't understand why because the WaveHC sample sketch uses 6 wave files. At this point I am just happy I can get the whole project to work even if it isn't what I wanted. Thank you for the input on my post. I am sure I will be posting more soon as I am a graduating electronics student and I am not a programmer. I hope your future input will help me become more of a programmer. Thank you very much. Included below is my functional sketch(except for the Serial.println statements).
#include <SD.h>
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
void setup()
{
Serial.begin(9600); //Setup Serial
pinMode(14, INPUT); //Assigns analog pins 0-4 as sensor inputs
pinMode(15, INPUT);
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(19, INPUT);
pinMode(6, OUTPUT); //Assigns digital pin 6 as output
pinMode(7, OUTPUT); //Assigns analog pin 7 as output for motor
pinMode(8, OUTPUT); //Assigns digital pin 8 as output for LED cluster 1
pinMode(9, OUTPUT); //Assigns digital pin 9 as output for LED cluster 2
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
// while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
// card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
//while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
// while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
}
void sdErrorCheck(void)
{
if (!card.errorCode())
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
// while(1);
}
void playfile(char *name) {
// see if the wave object is currently doing something
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
}
// ok time to play! start playback
wave.play();
}
void loop()
{
if(digitalRead(14) == HIGH)
{
// blinky=0;
playfile("howl.WAV");
while (wave.isplaying)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(100);
}
}
else if(digitalRead(15)==HIGH)
{
playfile("howl.WAV");
while(wave.isplaying)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(100);
}
}
else if(digitalRead(16)==HIGH)
{
digitalWrite(7,HIGH);
playfile("ghost.WAV");
while(wave.isplaying)
{
digitalWrite(8,HIGH);
delay(70); //set proper delay here for ghost to go to the end
digitalWrite(8,LOW);
delay(70);
}
}
else if(digitalRead(17)==HIGH)
{
digitalWrite(9,HIGH);
delay(60);
digitalWrite(9,LOW);
delay(60);
}
else if(digitalRead(19)==HIGH)
{
playfile("help.WAV");
while(wave.isplaying)
{
digitalWrite(9,HIGH);
delay(120);
digitalWrite(9,LOW);
delay(120);
}
}
delay(10);
}
Message me if you would like to know more about my sensors.
Serial.println statements do not work
They do for everybody else.
only 3 wave files can be included in the code
Highly unlikely, you have another problem.
I am not a programmer
And you never will be if you don't learn to debug code in a methodical manner.
I am a graduating electronics student
Debugging in a methodical manner is equally important for an electronics engineer.
posting more soon
Will you try what people suggest then?
Anyway I guess you have what you need for today, good luck with the course.
Rob
Graynomad:
Serial.println statements do not work
They do for everybody else.
only 3 wave files can be included in the code
Highly unlikely, you have another problem.
I am not a programmer
And you never will be if you don't learn to debug code in a methodical manner.
I am a graduating electronics student
Debugging in a methodical manner is equally important for an electronics engineer.
posting more soon
Will you try what people suggest then?
Anyway I guess you have what you need for today, good luck with the course.
Thanks for saying what I was about to say
Tim