Show Posts
Pages: [1] 2 3 ... 58
1  Using Arduino / Programming Questions / Re: Help with malfunctioning sketch - arduino keeps looping same "if" on: October 17, 2014, 01:51:52 pm
Those if statements have a lot of terms with && and || between them.  I would use some parenthesis (even if they're not strictly needed) to break those lines up into groups to make sure they're being evaluated the way you want them to be. 
2  Using Arduino / Programming Questions / Re: Catching stuck interrupts on: October 14, 2014, 08:58:23 am
The watchdog will reset the board for you if you fail to pat the dog in time.  If that's an acceptable solution to you then fine.

I think the better solution would be to look at the code and see if you can stop it from locking things up.
3  Using Arduino / Programming Questions / Re: Could someone please help me to use a rotary encoder with a Menu? on: October 09, 2014, 09:35:46 pm
I built a menu like that recently but unfortunately it's on my other computer. 

Basically what I did was to put all my menu entries into an array.  Then I hold a variable as an index to that array and it tells which menu item is selected.  The display code uses the same index to decide which one to display.  When the button is pressed it uses that same index in a switch case to decide what to do. 

I read my encoder with an interrupt and it simply increments or decrements a counter.  At the top of each pass through the loop I look at that counter and add it to the index and reset the counter back to zero.   I use a modulo expression to make sure the index doesn't go out of bounds. 

That's really it.  There's not much more to it. 

The only tricky part for me was learning to check the button before I check the encoder counter.  If the button was pressed then I ignore the encoder count for that loop.  That way when the button is pressed it runs the thing that is currently being displayed.  Otherwise I might be looking at the first menu item when I press the button, but then the display updates to show the second menu item which wasn't what I was looking at when I pressed the button. 
4  Using Arduino / Programming Questions / Re: Unable to exit from "for" loop on: October 09, 2014, 06:51:36 pm
for(j=-2; j<= 2; j++) strip.setPixelColor(pos+j, 0);

That semicolon at the end is probably going to be a problem.
5  Using Arduino / Programming Questions / Re: error compiling when trying to upload for a 8x8 led matrix on: October 09, 2014, 09:05:55 am
Can you post the library you're using?  It is complaining about a line in the constructor in the Adafruit_8x16matrix library, which isn't included in the code you posted.
6  Using Arduino / Programming Questions / Re: can't get speaker and led matrix to work at the same time - coding issue. on: October 08, 2014, 10:03:02 pm
If commenting those lines fixes things then...

Those lines are opening the wav file on the SD card.  IF you comment them out then this line:

if (!, name)) {return;}

in the playfile function catches true since the file isn't open and returns.  That means the wav file doesn't play because playfile returned before it started it, because it didn't have a wav file because the file wasn't open.  So the problem still has to be something to do with the playing of the wav file. 

How are you driving that speaker?  Could that be dragging the power down?
7  Using Arduino / Programming Questions / Re: Help with digitalRead(input pin). on: October 08, 2014, 09:47:13 pm
How is the switch wired?  Is it active HIGH or active LOW?
8  Using Arduino / Programming Questions / Re: wonky digitalRead and/or IF statement... on: October 08, 2014, 09:36:56 pm
I'm guessing this is your first time with a breadboard.  

The little holes together in vertical (EDIT: in the picture they are horizontal) groups of 5 going from the outside to the middle, they're all connected together.  Look up how it works.  

Right now, 5V, both ends of the resistor, the pin, and one side of your switch are all connected together.  So when you close the switch, that means 5V is connected directly to ground and the board shuts down and you lose the com port.  Fortunately the USB port on your computer is probably keeping you from frying anything.  
9  Using Arduino / Programming Questions / Re: Colour cross-fading with buttons forward and reverse on: October 08, 2014, 05:59:33 pm
The fading in this example is blocking.
On the other hand, since there are no delay()s, it'll rip through that for loop in nothing flat.

I did it that way on purpose.  You're right it won't block for long, but I didn't want the OP thinking that the delay call was what was causing it to block.
10  Using Arduino / Programming Questions / Re: Colour cross-fading with buttons forward and reverse on: October 08, 2014, 05:57:35 pm
It also doesn't help that I forgot to update previousMillis anywhere.   Going back to correct that now.
11  Using Arduino / Programming Questions / Re: Colour cross-fading with buttons forward and reverse on: October 08, 2014, 05:53:33 pm
Take these two codes for instance.  Let's look at the first one, the blocking example:

DISCLAIMER:  This code compiles but is untested.  Sorry if I made any mistake.

int blinkLedPin = 6;
int fadeLedPin = 5;

void setup(){
  pinMode(blinkLedPin, OUTPUT);
  pinMode(fadeLedPin, OUTPUT);

void loop(){
  digitalWrite(blinkLedPin, HIGH);
  for(int i = 0; i < 255; i++){
    analogWrite(fadeLedPin, i);
  digitalWrite(blinkLedPin, LOW);
  for(int i = 255; i>0; i--){
    analogWrite(fadeLedPin, i);

Follow down this code through the loop function.  First it turns one LED on, then it does what is in that for loop 255 times over, THEN it turns the blinking LED back off.  The fading in this example is blocking.  Nothing outside that for loop is going to happen until it has gone through the for loop 255 times.  So what you'd see is a light turn on, then the other one fades up, then the one turns off, then the other one fades back down.   Then repeat.

Now consider the non-blocking example.  Here nothing gets hung up in any loops.  The loop function keeps turning over and over and through each iteration we check to see if it is time to blink or time to do a fade step and if it is then we do one step and move on to running the rest of the code.  I know it doesn't do things exactly the same way, but it isn't as easy to write non-blocking code.  But follow through the loop function one line at a time thinking about what happens at each step, at no point are we stuck inside one part of the code without letting the rest of the code do its thing.  

unsigned long previousBlinkMillis = 0;
unsigned long previousFadeMillis = 0;

int fadeLevel = 0;
int fadeStep = 1;
int fadeDelay = 30;

int blinkDelay = 250;

int fadeLedPin = 5;
int blinkLedPin = 6;

int blinkLedState = LOW;

void setup(){
  pinMode(fadeLedPin, OUTPUT);
  pinMode(blinkLedPin, OUTPUT);

void loop(){
  unsigned long currentMillis = millis();
  if(currentMillis - previousBlinkMillis >= blinkDelay){
    blinkLedState = !blinkLedState;
    digitalWrite(blinkLedPin, blinkLedState);
    previousBlinkMillis = currentMillis;
  if(currentMillis - previousFadeMillis >= fadeDelay){
    fadeLevel = fadeLevel + fadeStep;
    analogWrite(fadeLedPin, fadeLevel);
    if(fadeLevel == 255 || fadeLevel == 0){
      fadeStep = -fadeStep;
    previousFadeMillis = currentMillis;

Now, using those two examples, can you see the difference?  Do you think you could write a non-blocking code to do what you want?
12  Using Arduino / Programming Questions / Re: i have a question about an led strip code from radio shack on: October 08, 2014, 05:30:57 pm
Does that function get called anywhere else in the code that you do want to keep?  If not then go ahead and delete it. 
13  Using Arduino / Programming Questions / Re: i have a question about an led strip code from radio shack on: October 08, 2014, 01:50:33 pm
Looks like most of it is already commented out. 
14  Using Arduino / Project Guidance / Re: 7 segment display using 3 shift registers 74HC595 on: October 08, 2014, 12:11:43 pm
if( value & (0x01<<i)  )

Don't know if it is all of your problem but this line is broken.  0x01<<i will be zero for any i larger than 15. 

This would be better by forcing the 1 to be an unsigned long so it will have 32 bits to shift to.

if( value & (0x01UL<<i)  )
15  Using Arduino / Programming Questions / Re: Programming questions on DC motor on: October 08, 2014, 09:45:19 am
Where in that for loop are you even trying to read the motion sensor? 
Pages: [1] 2 3 ... 58