advise on programming procedure needed

Hello,

I have recently bought on arduino ethernet and I have no experience of coding. I have planned a few projects on paper. I know what I want to do but I dont know the procedure of putting ideas on paper into code and the ordering of the code. A few questions:

  1. when you guys have an idea and have wrote down what you want the thing to do, how do you go about putting the idea into code and where do you start?

  2. When creating while loops, do you put if statements inside the while loop or outside?

  3. When you want to create multiple loops do these loops have to be inside each other to break out of one to enter another?

Thanks in advance

  1. when you guys have an idea and have wrote down what you want the thing to do, how do you go about putting the idea into code and where do you start?

That depends on how well you have written down what you want to do, and how well you understand how to map that to C++ statements. Generally, I start with making one part at a time work. Read from some sensors. Then, send that data somewhere. Then, use that data. Then, generate a response. Then, use that response.

  1. When creating while loops, do you put if statements inside the while loop or outside?

Yes. Exactly where depends on why there is a while loop, and what the tests are for.

  1. When you want to create multiple loops do these loops have to be inside each other to break out of one to enter another?

Depends on what the loops do. No one size fits all answer exists.

so if you have for example:

while(something1 and something2 == low)
{
if something1 == high {break out of the loop and go to function1 }
if something2 == high { break out of the loop and go to function2}
}

//once the function 1 or 2 is run go into next while loop

while(something1 and something2 == low)
{
if something1 == high {break out of the loop and go to function3 }

if something2 == high { break out of the loop and go to function4}
}

so if you have for example:

while(something1 and something2 == low)
{
if something1 == high {break out of the loop and go to function1 }
if something2 == high { break out of the loop and go to function2}
}

I think you mean:

while(something1 == LOW && something2 == LOW)
{
}

Notice that inside the while loop, you need to determine if something1 and something2 have changed. Otherwise the while loop will never end, and, of course, the if tests will never be true.

In C/C++, you do not go to functions. You call them. Calling function1 or function2 does not necessarily require breaking out of the while loop.

Leebranch921:
Hello,

I have recently bought on arduino ethernet and I have no experience of coding. I have planned a few projects on paper. I know what I want to do but I dont know the procedure of putting ideas on paper into code and the ordering of the code. A few questions:

  1. when you guys have an idea and have wrote down what you want the thing to do, how do you go about putting the idea into code and where do you start?

  2. When creating while loops, do you put if statements inside the while loop or outside?

  3. When you want to create multiple loops do these loops have to be inside each other to break out of one to enter another?

Thanks in advance

1 Code first documentation second maybe some white boarding in the middle. Since it no longer takes hours to compile there is really no penalty for getting to code early.

2 Inside, though I almost never use a while loop.

3 Again never really seen a reason to spin around in separate loops on a micro. I have a tendency to get all my inputs process them and then act upon them while using ints for anything that's time critical.

Try being more specific as to what your trying to do.

So if I break out of a while loop and there is a another while loop after it will the program be in the next while loop?

If you dont use while loops, what do you use?

What are you using while loops to do?

Have you looked at any of the Arduino example sketches?

Leebranch921:
So if I break out of a while loop and there is a another while loop after it will the program be in the next while loop?

If you dont use while loops, what do you use?

The break keyword only breaks out of the inner loop. If you need to break out further, there is goto, which is generally frowned upon because it makes the code hard to follow (like some breadboard pictures I've seen with wires going everywhere), and return which exits the current function.

Generally once a project is more than a few lines, what I do is break things into functions that take some parameters and maybe produce one or more results. A function is a set of canned moves that allows you to break the task into smaller items.

Unfortunately, I tend to think internet forums like this aren't that good for teaching the basic concepts. Once you have the concepts, forums are good at things of syntax issues, etc. Is there a community college nearby that offers an intro to programming class? Maybe a hackerspace? Or a local techy teenager, that hasn't taken to sneering at his/her elders?

I would try to step back, and try to block out what you want to do at a high level (not worrying about language). Initially, you want the project to not have all of the bells and whistles that you eventually will want, but you want a reduced project to get you started. You will eventually add these later.

Now, that you have a high level idea, pick one part of the whole project to work on. Now, in this smaller piece, you want to iterate with successive refinement until you have that one piece done. Then save it, and carefully document the wiring setup (take one or more pictures of the breadboard, and write out a schematic so that you can replicate this later. One thing that I just ordered was several prototype shields with mini-breadboards, so that I could remove one shield and replace it with another. Then go on to the next thing. If you try to create the whole thing at once, it is a recipe for failure.

In addition to saving pictures and schematics, be sure to save your code. Backup early, backup often, particularly off of your primary computer.

Unfortunately, I tend to think internet forums like this aren't that good for teaching the basic concepts.

Agreed. But, if you have one specific project in mind, this forum is a great way to go from "Can I ..." to "See what I did..." in remarkably short order.

I have purchased beginning arduino and Ive gone through the light tutorials like blinking, traffic lights without and with the button. I am trying to figure out how to control the code. I first tried having 2 lights and 2 buttons, that produce a green light for the sequence 12 and a red light for 21.

I tried to use the sequence 11 but it wont happen. say if I press 1 to go to void green, how do I stop the state of button1 being 1? I put a Serial.print(P1); at the start of the function and it read the state as 1 like I have my finger on the button and not 0 like it should be.

how do I stop the state of button1 being 1?

Stop pushing it.

I put a Serial.print(P1); at the start of the function and it read the state as 1 like I have my finger on the button and not 0 like it should be.

Then you have an issue with how the switch is wired. Most likely, you have a floating pin condition. How IS the switch wired?

The simplest way is to activate the internal pullup resistor, connect one leg of the switch to a digital pin, connect the other leg to ground, and expect LOW to be pressed, and HIGH to be released.

yeah the buttons are wired like you say, if I press button1 and let go, it calls the function green and turns on led1 but Serial.print(P1); showed the state as 1 when I wasnt touching it when it should be 0.

it calls the function green and turns on led1 but Serial.print(P1); showed the state as 1 when I wasnt touching it when it should be 0.

Show your code.

Here is the code, at void state11, before the if(P1 == HIGH), the state of the switch is 1 when it should be 0.

int L1  = 13;
int L2  = 12;
int GREEN  = 11;
int RED  = 10;
int BUTTON1  = 7;
int BUTTON2 = 6;

void setup()
{
  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  Serial.begin(9600);
}

void loop()
{
  int P1 = digitalRead(BUTTON1);
  int P2 = digitalRead(BUTTON2);
  
  while(P1 && P2 == LOW)
  {
    int P1 = digitalRead(BUTTON1);
    if(P1 == HIGH)
    {
      state11();
    }
    
    int P2 = digitalRead(BUTTON2);
    if(P2 == HIGH)
    {
      state21();
    }
  }
}

void state11()
{
  int P1 = digitalRead(BUTTON1);
  int P2 = digitalRead(BUTTON2);
  digitalWrite(L1, HIGH);
  if(P1 == HIGH)
  {
    digitalWrite(L2, HIGH);
    delay(2000);
    digitalWrite(L1, LOW);
    digitalWrite(L2, LOW);
    digitalWrite(GREEN, HIGH);
    delay(2000);
    digitalWrite(GREEN, LOW);    
  }
  if(P2 == HIGH)
  {
    digitalWrite(L2, HIGH);
    delay(2000);
    digitalWrite(L1, LOW);
    digitalWrite(L2, LOW);
    digitalWrite(RED, HIGH);
    delay(2000);
    digitalWrite(RED, LOW);    
  }
}

void state21()
{
  int P1 = digitalRead(BUTTON1);
  int P2 = digitalRead(BUTTON2);
  digitalWrite(L1, HIGH);
  if(P1 == HIGH || P2 == HIGH)
  {
    digitalWrite(L2, HIGH);
    delay(2000);
    digitalWrite(L1, LOW);
    digitalWrite(L2, LOW);
    digitalWrite(RED, HIGH);
    delay(2000);
    digitalWrite(RED, LOW);    
  }
}
  while(P1 && P2 == LOW)

There are no shortcuts for comparing two values to LOW. If that is what you are trying to do, you need:

  while(P1 == LOW && P2 == LOW)
  int P1 = digitalRead(BUTTON1);
  int P2 = digitalRead(BUTTON2);
  
  while(P1 && P2 == LOW)
  {
    int P1 = digitalRead(BUTTON1);
    if(P1 == HIGH)

I would STRONGLY discourage you from using variables with the same name in nested scopes.

void setup()
{
  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  Serial.begin(9600);
}

Where have you enabled the pullup resistors?

not sure what you mean by pull-up resistors, on the breadboard there is a 150ohm resistor connected between one of the button pins and ground.

The simplest way is to activate the internal pullup resistor, connect one leg of the switch to a digital pin, connect the other leg to ground, and expect LOW to be pressed, and HIGH to be released.

yeah the buttons are wired like you say

not sure what you mean by pull-up resistors, on the breadboard there is a 150ohm resistor connected between one of the button pins and ground.

What we have here is a failure to communicate. The switches are either wired expecting the pullup resistor to be enabled, or they are not.

Get rid of the weak 150 ohm resistor. Connect one leg of the switch to the digital pin. Connect the other leg to ground. In setup(), after the pinMode() call for a given INPUT pin, add

digitalWrite(somePin, HIGH); // Turn on pullup resistor

is thats the way its done? the book showed a 150ohm res used.

Get rid of the weak 150 ohm resistor

I'd say 150 was pretty meaty, but yes, get rid of things that costs you money, and use the free stuff.

so what does this mean then if I use this pullup resistor, Im new so not totally sure about some things.