Go Down

Topic: Millis, repeat code. how to make it work????? (Read 1 time) previous topic - next topic

Theelectronicguy

Hello everyone,

I have basically two problems.First, I want to repeat a line of code in the "if" statement a number of times.
Second, I want a led to turn on according to the sensor value at the same time the rest of the code runs. I already looked at the blinkwithouthdelay example but I don't understand how to use it with "if" and "else".

Here's the code:
Code: [Select]

const int m1r=3;
const int m1l=4;
const int m2f=9;
const int m2b=11;
const int threshold = 200;
const int threshold1= 900;
const int sensorMin = 100;

void setup(){
   pinMode(m1r, OUTPUT);
    pinMode(m1l, OUTPUT);
     pinMode(m2f, OUTPUT);
      pinMode(m2b, OUTPUT);
}
void loop(){
  int analogValue = analogRead(0);
     int analogValue1 = analogRead(1);
  //Motor control   
if (analogValue > threshold) {
 
   
     digitalWrite(m2b,LOW);
  digitalWrite(m2f,HIGH);
 
   delay(1000);
   [b]//Here I want to repeat the following lines six times[/b]
   
   digitalWrite(m1r,HIGH);
   digitalWrite(m1l,LOW);
   
   digitalWrite(m2b,LOW);
   digitalWrite(m2f,HIGH);

   
   delay(700);
   
   digitalWrite(m2b,HIGH);
   digitalWrite(m2f,LOW);
   
   digitalWrite(m1r,LOW);
   digitalWrite(m1l,HIGH);
   
   delay(1000);
   
    digitalWrite(m2b,HIGH);
   digitalWrite(m2f,LOW);
   
   digitalWrite(m1r,LOW);
   digitalWrite(m1l,HIGH);
   
   delay(1000);
   
  }

[b]//Until here[/b]
   
else{
 
  digitalWrite(m2f,LOW);
  digitalWrite(m2b,HIGH);
digitalWrite(m1r,HIGH);
   digitalWrite(m1l,HIGH);
}

}


An example would be appreciated.

CrossRoads

add at top:
int x = 0;

while (x <7){
your code that repeats

x=x+1;
}
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Onions

Quote
First, I want to repeat a line of code in the "if" statement a number of times.


This should be easy, just use a for loop:
http://arduino.cc/en/Reference/For

You just write
for([variable used to count]; [counting variable][comparing statement][maximum value]; [increment]){
      repeated statement
}

For the variable used to count, I usually just type "int i = 0", where 0 is there the counting starts from.
The comparing statement is things like >,  <,  >=,  <=,  != ...
Maximum value is the number that the counting variable will be compared to. The for loop will stop when the statement is true.
Increment is the ammount you are adding to the counting variable.

for a for loop, you will use:

for(int i =0; i < 4; i++){
(code)
}

This will run the code block until i is greater than four.

I hope this helps, not makes it more confusing. :smiley-fat:
Onions.
My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) :D

Onions

#3
Mar 03, 2011, 07:16 pm Last Edit: Mar 03, 2011, 07:19 pm by Onions Reason: 1
CrossRoads, you posted yours as I was typing mine!  XD XD XD (That's the second time this has happened!).

Your code looks easier, and is easier to undeerstand than my bad explanation.   :smiley-roll: :~
Onions.
My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) :D

CrossRoads

Just trying to stay ahead of the gang!

For:next, while loop; same functionality, different method of writing it.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

CrossRoads

Need to reset x for the next pass thru also, forgot add that:

while (x <7){
your code that repeats

x=x+1;
}
x=0; // reset for next pass thru loop.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Theelectronicguy

Thanks for the quick replies.

I tried it and it worked. Now I just want to know how to run simultaneously a code that turns on or off a led based on the value of the sensor and I can get on with my life :). I don't know how to use the blinkwithuthdelay example with an "if" statement.

CrossRoads

Analog sensor? Digital sensor? where do you want it to turn on/off - outside of the while loop?
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Theelectronicguy

Analog sensor.

I figured it out.

Thanks for all the help.

Joonazan

#9
Mar 03, 2011, 08:57 pm Last Edit: Mar 03, 2011, 08:59 pm by Joonazan Reason: 1
I have written a library that should help with this.
Try something like this:
Code: [Select]

#include <"Timing.h">

class StateMachine
{
 int state = 1;
 public:
 void next()
 {
   switch(state){
   case 1: digitalWrite(13,HIGH); events.add(1000,next); break;
   case 2: digitalWrite(13,LOW); events.add(1000,next); break;
   default: state = 1;}
   state++;
 }
};

// You also need some code that uses this class


For example the for loop would be replaced with:
Code: [Select]

if(state < 4){something; state++;}


EDIT: You posted while I was writing. Use this code if you add another thing with delays.

Go Up