Please help

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Re: Please help

Postby adafruit_support_bill » Mon Aug 22, 2011 7:15 pm

The nesting levels of your curly braces '{' & '}' is not the same in the last two sketches you posted. That changes the logic considerably. Note the differences here:

Code: Select all
         digitalWrite(led1Pin, HIGH);
         delay(timer);
      }   
    }}

vs. here:
Code: Select all
         digitalWrite(led1Pin, HIGH);
         delay(timer);
      }   
    }}}

It would be a lot easier to spot such things if you take some of ChuckM's advice and re-structure the code into manageable pieces.
User avatar
adafruit_support_bill
 
Posts: 16644
Joined: Sat Feb 07, 2009 9:11 am

Re: Please help

Postby Spinsycle » Mon Aug 22, 2011 7:30 pm

I am looking to simplify the code. I have been reading about array's for the past hour try'n to understand it. its only different because before i upload any code to the board i compile it and it gave me an error message. putting in the extra curly bracket got rid of the error message and let it compile
Spinsycle
 
Posts: 10
Joined: Sun Jul 31, 2011 3:21 pm
Location: New York

Re: Please help

Postby adafruit_support_bill » Mon Aug 22, 2011 7:44 pm

Getting code to compile and getting the logic correct are not necessarily the same. You needed an extra curly brace because they need to be balanced overall. But the correct place to insert it was probably back where you inserted the extra if/else clause.

These things are really difficult to spot when they are nested so deeply. Things that will make the logic easier to follow are:
    Use a 'switch' statement instead of a deeply nested if/else as suggested by ChuckM
    Be consistent about how you indent your code & braces.
User avatar
adafruit_support_bill
 
Posts: 16644
Joined: Sat Feb 07, 2009 9:11 am

Re: Please help

Postby Spinsycle » Mon Aug 22, 2011 7:59 pm

Getting code to compile and getting the logic correct are not necessarily the same. You needed an extra curly brace because they need to be balanced overall. But the correct place to insert it was probably back where you inserted the extra if/else clause.


thank you i will look into that.

I think my brain is gunna start blinking soon.
Spinsycle
 
Posts: 10
Joined: Sun Jul 31, 2011 3:21 pm
Location: New York

Re: Please help

Postby ChuckM » Mon Aug 22, 2011 8:26 pm

Simplifying the code will help you quite a bit I'm sure, for example the code you wrote for the button press is below (I've re-formatted it to use a 'regular' indent pattern.

Code: Select all
    val2 = digitalRead(switchPin);     // read the input again to check for bounces
    if (val == val2) {                 // make sure we got 2 consistant readings!
        if (val != buttonState) {          // the button state has changed!
            if (val == LOW) {                // check if the button is pressed
                if (lightMode == 0) {          // if its off
                lightMode = 1;               // make it all on!
            } else {
                if (lightMode == 1) {        // if its all on
                    lightMode = 2;             // make it blink!
                } else {
                    if (lightMode == 2) {      // if its blinking
                    lightMode = 3;           // make it wave!
                    } else {
                        if (lightMode == 3) {    //  if its waving,
                            lightMode = 4;         // make it dubble wave!
                        } else {
                            if (lightMode == 4) {  // if its dubble waving
                                lightMode = 5;       // make it reverse wave!
                            } else {
                                if (lightMode ==5) {
                                    lightMode =0;
                                }
                            }
                        }
                    }
                }
            }
            buttonState = val;                 // save the new state in our variable
        }


Something to be aware of is that the last brace here (the } character) is still two braces short of closing off the top level "if" statement. What is worse, I don't believe this code is doing what you think it is doing.

This code is what switch statements were designed for, the form of a switch statement is as follows:

Code: Select all
switch (<expression>) {
case <value-1>:
                <statements>;
                break;
case <value-2>:
                <statements>;
                break;
case <value-n>:
                <statements>;
                break;
default:
              <statements>;
}


In English, the compiler evaluates the expression that is inside the parenthesis and gets back a number. Then it looks for a label of the form "case n:" where n is the same number, so if the expression evaluated to the number 1 it would look for "case 1:" and then it does a 'goto' to that label and starts running the statements it finds. Until it sees the statement 'break' at which point it jumps all the way down to the next statement after the closing brace. Finally, the "special" label is default: this tells the compiler that if you can't find the value in the various case statements, do the one labelled default.

So for your bit of code you probably wanted to write:

Code: Select all
switch (lightMode) {                // 'lightMode' is the expression here
    case 0: lightMode = 1;          // if it was 0 make it 1
               break;
    case 1: lightMode = 2;          // if it was 1 make it 2
               break;
    case 2: lightMode = 3;          // if it was 2 make it 3
               break;
    case 3: lightMode = 4;          // if it was 3 make it 4
               break;
    case 4:  lightMode = 0;          // if it was 4 make it go back to 0
               break;
    default: lightMode = 0;           // if it was anything else, make it 0
               break;
}


If you used a switch statement (and thus simpler braces and indentation) you would see that your program (as written) has unbalanced braces and, as written, won't do anything unless you hold the button down.

--Chuck
ChuckM
 
Posts: 139
Joined: Thu Dec 24, 2009 2:31 am

Re: Please help

Postby Spinsycle » Mon Aug 22, 2011 8:42 pm

Thank you i really appreciate the help. because im new i dont know when to use an if statement and when to use array's and switch statement. im gunna try all this out tomorrow.
Spinsycle
 
Posts: 10
Joined: Sun Jul 31, 2011 3:21 pm
Location: New York

Re: Please help

Postby adafruit_support_bill » Tue Aug 23, 2011 4:44 am

Actually, the whole 'lightMode' logic could be simplified even further:

Code: Select all
    lightmode = lightMode + 1; // increment the light mode
    if (lightMode > 4)   // if it exceeds the maximum
    {
        lightMode = 0;  // reset to zero
    }
User avatar
adafruit_support_bill
 
Posts: 16644
Joined: Sat Feb 07, 2009 9:11 am

Re: Please help

Postby ChuckM » Tue Aug 23, 2011 7:05 pm

And even easier (but more complicated to read)

Code: Select all
lightMode = (++lightMode) % 5;


Sadly its terribly easy to obfuscate with C, can be fun if you know it well, sort of like telling bawdy puns in English.

That being said, if it is a variation on a state machine then the simplest implementation in C is the array so

Code: Select all
int next_states[] = {1, 2, 3, 4, 0 };

lightMode = next_state[lightMode];


Which maintains a lot of flexibility since you can go to any state from any state, but has the downside that if lightMode becomes corrupted and falls outside the range of allowed states then you are in trouble. Also if you wanted a sparse set of state values you can waste a bit of space with an array which, by neccesity, has memory between its minimum and maximum index.

Don't worry Spin, none of this is critical for your issues at the moment, but later you may find it useful.

--Chuck
ChuckM
 
Posts: 139
Joined: Thu Dec 24, 2009 2:31 am

Re: Please help

Postby adafruit_support_bill » Tue Aug 23, 2011 7:41 pm

And even easier (but more complicated to read)

Best to stick with the basics at this stage. It may be fewer keystrokes, but if it is complicated to read, it really isn't easier now is it? :D
User avatar
adafruit_support_bill
 
Posts: 16644
Joined: Sat Feb 07, 2009 9:11 am

Previous

Return to Arduino Starter Pack

Who is online

Users browsing this forum: No registered users and 2 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [113]

Raspberry Pi[82]
 
FLORA[24]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[12]
Arduino[60]
 
NETduino[14]
 
BeagleBone[23]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[39]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[9]


 
Breakout Boards[35]
LCDs & Displays[49]
Components & Parts[70]
Batteries & Power[54]
EL Wire/Tape/Panel[52]
LEDs[112]
 
Wireless[16]
Cables[66]
 
Lasers[6]
Sensors/Parts[147]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[41]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[25]


 
Stickers[41]
 
Skill badges[55]
 
Books[26]
 
Circuit Playground[7]
 
Gift Certificates[4]