Pages: [1]   Go Down
Author Topic: blink W/O delay (sorry)  (Read 1030 times)
0 Members and 1 Guest are viewing this topic.
Massachusetts, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Things fall apart, it's scientific
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

First I'd like to apologize for my ineptitude. Programming is new to me and it would seem I'm learning very slowly. From the start I'd like to thank anyone who is kind enough to field this very basic (and no doubt silly) question.

Second, this is an extension of a previous post were I was encouraged to stop using "delay" for reasons that make a good deal of sense to me.

I would like to use the "blink W/O delay" example to pause or continue an action (in this case it would be 'keep going straight' or 'keep turning' or what have you) I had been doing this:

if (digitalRead(switchPin) == HIGH) {
    digitalWrite(motorPin1, LOW);     //set L1 low
    digitalWrite(motorPin2, HIGH);    //set L2 high
    digitalWrite(motorPin3, LOW);     //set L3 low
    digitalWrite(motorPin4, HIGH);    //set L4 high
    delay(2000);
    digitalWrite(motorPin1, HIGH);   //set L1 low
    digitalWrite(motorPin2, LOW);    //set L2 high
    digitalWrite(motorPin3, LOW);    //set L3 low
    digitalWrite(motorPin4, HIGH);   //set L4 high
    delay(1500);

I have tried a number of times attempting to work the "blink w/o delay" method here and simply can't find a working combination. I have read every explanation I have been able to locate.

If someone could please give this newbie a hand up it would greatly appreciated. Thanks in advance for kind assistance.

Logged

Waterloo, Canada
Offline Offline
Full Member
***
Karma: 1
Posts: 242
Engineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there some part of the BlinkWithoutDelay tutorial that is confusing still?  I think if we can help your understanding of this  principle you could then easily apply it to your specific problem.

ref: http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

In short... the BlinkWithoutDelay main loop checks for a condition where the interval has expired.  If it has it toggles the output.  If it hasn't it continues on to perform other tasks (of which there are none in the tutorial).
I presume it's been pointed out to you already that using delay() by contrast ties up the processor preventing it from performing any other tasks.
Logged

Massachusetts, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Things fall apart, it's scientific
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It must be frustrating answering questions from absolute neophytes. If there's one thing I can't stand myself is someone who asks for help without first reading the available material.

I can make the led blink, and more or less understand why. I also understand why 'delay' is not the best method for doing this.

What I haven't been able to understand is where this might fit into my simplistic attempt at making my bot continue turning or moving forward.

I understand you are busy, and no doubt you've had it with newbies, but if you could soldier on and help me understand it would be greatly appreciated.

Here's what I am doing:



int IRPin2 = 1;          // select input pin for the second IR sensor
int IRPin2Value = 0;    //store value from IR sensor 2
int IRPin = 2;         //select the input pin for the IR sensor
int IRPinValue = 0;   //store value from IR sensor
int switchPin = 2;   //first bumper switch, digital return
int switchPin2 = 5; //second bumper switch, digital return
int motorPin1 = 3;   //h-bridge L1
int motorPin2 = 4;    //h-bridge L2
int motorPin3 = 7;     //h-bridge L3
int motorPin4 = 6;      //h-bridge L4
int speedPin = 9;        //enable 1 (E on the L1/L2 side)
int speedPin2 = 10;       //enable 1 (E on the L3/L4 side)
long blinkTimer = 0;
int blinkInterval = 1000;


void setup()

{
  
  pinMode(switchPin, INPUT);  //set the bumper switch  as an input
  pinMode(switchPin2, INPUT); //set the bumper switch 2 as an input
  
  //pinMode(IRPin, INPUT); // IRpin as input
  //pinMode(IRPin2, INPUT);// IRpin2 as input
  //apparently the analog in does not need to be called
  
  //set the following pins as outputs:
  
  pinMode(motorPin1 , OUTPUT);
  pinMode(motorPin2 , OUTPUT);
  pinMode(motorPin3 , OUTPUT);
  pinMode(motorPin4 , OUTPUT);
  
  
  pinMode(speedPin , OUTPUT);  //this is setting one enable pin
  pinMode(speedPin2 , OUTPUT); //this is setting the other
  
  
  //set speedPin high so motors will run at start
  
  digitalWrite(speedPin, HIGH);
  digitalWrite(speedPin2, HIGH);
  
                            
}

void loop()

{
  
  //Threshold info and instructions for both IR sensors
  
  int threshold = 225;
  if(analogRead(IRPin) > threshold) {
    digitalWrite(speedPin2, LOW);
    digitalWrite(speedPin, LOW); }
    
  else if(analogRead(IRPin2) > threshold) {  //instructions given
    digitalWrite(speedPin2, LOW);            //for either IR sensor
    digitalWrite(speedPin, LOW); }           //condition (currently
                                             //stop and go)
  else{digitalWrite(speedPin2, HIGH);
       digitalWrite(speedPin, HIGH);
}



    
  //first bumper switch   [glow]I have tried to use the 'blink w/o delay" here for the first bumper switch, what I am looking for are the results achieved on the second bumper switch[/glow]
 
 {

if (millis() - blinkTimer >=blinkInterval /2 )

if (millis() - blinkTimer >=blinkInterval )

  {
  
  if (digitalRead(switchPin) == HIGH) {
    digitalWrite(motorPin1, LOW);     //set L1 low
    digitalWrite(motorPin2, HIGH);    //set L2 high
    digitalWrite(motorPin3, LOW);     //set L3 low
    digitalWrite(motorPin4, HIGH);    //set L4 high

   blinkTimer = millis();
  }

    digitalWrite(motorPin1, HIGH);   //set L1 low
    digitalWrite(motorPin2, LOW);    //set L2 high
    digitalWrite(motorPin3, LOW);    //set L3 low
    digitalWrite(motorPin4, HIGH);   //set L4 high
    
   blinkTimer = millis();
    
    
     }
  
  //second bumper switch [glow]this is working as I would like, but with the nasty 'delay' in place[/glow]
    
 else if (digitalRead(switchPin2) == HIGH) {
    digitalWrite(motorPin1, LOW);  //set L1 low
    digitalWrite(motorPin2, HIGH); //set L2 high
    digitalWrite(motorPin3, LOW);  //set L3 low
    digitalWrite(motorPin4, HIGH); //set L4 high
    delay(10000);
    digitalWrite(motorPin1, HIGH); //set L1 low
    digitalWrite(motorPin2, LOW);  //set L2 high
    digitalWrite(motorPin3, LOW);  //set L3 low
    digitalWrite(motorPin4, HIGH); //set L4 high
    delay(5000);
    
     }
  
  //instructions given these conditions at either bumper
  
  else {
    digitalWrite(motorPin1, HIGH); //set L1 high
    digitalWrite(motorPin2, LOW);  //set L2 low
    digitalWrite(motorPin3, HIGH); //set L3 high
    digitalWrite(motorPin4, LOW);  //set L4 low
    
  }

}

Thank you in advance for your kind assistance!

    


Logged

Waterloo, Canada
Offline Offline
Full Member
***
Karma: 1
Posts: 242
Engineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No problem jscott.  If helping neophytes frustrated me, I wouldn't. smiley
If my post sounded short on patience I didn't mean it to.

Help me understand if this is what you want to do (pseudo-code)...

if (BumpSensor1 detected) {
  TurnDirectionAFor10Seconds();
  Drive();
}

if (BumpSensor2 detected) {
  TurnDirectionBFor10Seconds();
  Drive();
}

Is that the idea?
In that case you want the "BlinkWithoutDelay" logic to control when you switch from turning to driving.

Then the pseudo-code would be like this...

loop() {
 if (BumpSensor detected) {
  startTurning();
  myTurnTimer = millis();
 }

 if (millis() - myTurnTimer > TenSeconds) {
  Drive();  // note you might want a flag to check if you are already driving to avoid doing this on every iteration of loop()
 }

 DoOtherStuff();
}
Logged

Massachusetts, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Things fall apart, it's scientific
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps it was me being too thin skinned. I'm very sorry for the misunderstanding. I am the one who is frustrated. It surprises me how difficult this has been.

Yes, that is what I intend to do. Thank you for your help. I think I understand now. I will continue with this project tonight and get back to you.

I will post a question about basic texts and see if I can't get some more to read.

You have been quick to respond and kind to help. Thank you!
Logged

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

jscott,

If every novice were as polite and considerate as you, this forum would be a much happier place.  Best of luck on your project.

Regards,

-Mike
Logged

Massachusetts, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Things fall apart, it's scientific
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you. My Mom would've been proud.

Good manners, that I understand. Programming, maybe not so much smiley-wink
Logged

New Zealand
Offline Offline
Full Member
***
Karma: 2
Posts: 120
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey jscott,
I'm a novice too (don't even have an Arduino yet  :smiley), but I notice that one of the if loops didn't have a { after it, maybe this could be adding to the problem?

Code:
//first bumper switch   I have tried to use the 'blink w/o delay" here for the first bumper switch, what I am looking for are the results achieved on the second bumper switch

{

[glow]if (millis() - blinkTimer >=blinkInterval /2 )

if (millis() - blinkTimer >=blinkInterval )[/glow]
 {
 
 if (digitalRead(switchPin) == HIGH) {
   digitalWrite(motorPin1, LOW);     //set L1 low
   digitalWrite(motorPin2, HIGH);    //set L2 high
   digitalWrite(motorPin3, LOW);     //set L3 low
   digitalWrite(motorPin4, HIGH);    //set L4 high

  blinkTimer = millis();
 }

   digitalWrite(motorPin1, HIGH);   //set L1 low
   digitalWrite(motorPin2, LOW);    //set L2 high
   digitalWrite(motorPin3, LOW);    //set L3 low
   digitalWrite(motorPin4, HIGH);   //set L4 high
  
  blinkTimer = millis();
  
  
    }
 
« Last Edit: December 16, 2009, 06:09:33 pm by mattd » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley
Logged

Massachusetts, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Things fall apart, it's scientific
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

mattd,

Yes, those pesky brackets! Today I am looking through a C++ text I downloaded (free, yippee). Hopefully I will learn some definitive rules for using them. Nothing wrong here that a little reading won't fix.

Good eyes, thanks for the help!

WaveZa,

 smiley back at ya friend!
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
some definitive rules for using them

only one rule - they have to be the same number of closing braces as opening ones.
Too many do not matter, too few the code will not do what you want but will only do what you specify.
Logged

Massachusetts, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Things fall apart, it's scientific
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It couldn't be much simpler than that. Thank you!
Logged

Pages: [1]   Go Up
Jump to: