Pages: [1]   Go Down
Author Topic: Millis, repeat code. how to make it work?????  (Read 1317 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27145
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

add at top:
int x = 0;

while (x <7){
your code that repeats

x=x+1;
}
Logged

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.

Newcastle, England
Offline Offline
Sr. Member
****
Karma: 3
Posts: 489
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) smiley-grin

Newcastle, England
Offline Offline
Sr. Member
****
Karma: 3
Posts: 489
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

CrossRoads, you posted yours as I was typing mine!  smiley-lol smiley-lol smiley-lol (That's the second time this has happened!).

Your code looks easier, and is easier to undeerstand than my bad explanation.   smiley-roll smiley-confuse
Onions.
« Last Edit: March 03, 2011, 01:19:06 pm by Onions » Logged

My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) smiley-grin

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27145
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just trying to stay ahead of the gang!

For:next, while loop; same functionality, different method of writing it.
Logged

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.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27145
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley. I don't know how to use the blinkwithuthdelay example with an "if" statement.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27145
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Analog sensor? Digital sensor? where do you want it to turn on/off - outside of the while loop?
Logged

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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Analog sensor.

I figured it out.

Thanks for all the help.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have written a library that should help with this.
Try something like this:
Code:
#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:
if(state < 4){something; state++;}

EDIT: You posted while I was writing. Use this code if you add another thing with delays.
« Last Edit: March 03, 2011, 02:59:29 pm by Joonazan » Logged

Pages: [1]   Go Up
Jump to: