Pages: [1]   Go Down
Author Topic: Help with a while loop  (Read 306 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 3
Posts: 259
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This sure is not basic could any one show the right way to write this
Code:
void loop(){
    int x = 0;
  while(RX == HIGH; && x < 8;){
      delay(1000);
      x = ++x;
  }
Logged

Offline Offline
Edison Member
*
Karma: 49
Posts: 1676
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Get rid of the semicolons in the while condition and just to be safe parenthesize the conditions:
Code:
  while((RX == HIGH) && (x > 8)) {

Pete
Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If it was my code I would write it like this:
Code:
void loop()
{
  uint8_t x = 0;
  while( RX == HIGH && x < 8 )
  {
    delay( 1000 );
    x++;
  }
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If it was my code I'd write it like this:

Code:
void loop()
{
    unsigned long start = millis();
    while((millis() - start < 8000UL) && (RX == HIGH))
    {
    }
    // when we get here RX is low or 8000ms has elapsed
}

The advantage over the original code is that changes to RX are detected as soon as possible rather then only once per second. I assume you are using an interrupt to update RX.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Sr. Member
****
Karma: 3
Posts: 259
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I done the same code in swordfish basic  but can't get the loops right

This is what I'm trying to do I have a 315Mhz RF link kit The transmitter is on a pic 10F200 with three button switches .

You press  sw1 and it sends a 8mS high then a low of 8mS then 8 1mS pulses.

I want to get the start pulses
I tried some thing like this
Code:
int RX = 3;
void setup(){
  Serial.begin(9600);
  pinMode(RX, INPUT);
}

void loop(){
   Serial.println("Here we go again");
     unsigned long start = millis();
         while((millis() - start < 8000UL) && (RX == HIGH)){
   Serial.println("Here we where not");
    delay(100);
         }
}
« Last Edit: November 18, 2012, 05:08:34 pm by be80be » Logged

Offline Offline
God Member
*****
Karma: 19
Posts: 787
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int RX = 3;
void setup(){
...
  pinMode(RX, INPUT);
}

void loop(){
   ...
         while((millis() - start < 8000UL) && (RX == HIGH)){
 
         }
}

I think HIGH is defined to be 1, so it will never equal 3 and your while loop will never loop.  The expression will be false every time. 

Maybe you meant to do a digitalRead on that pin instead of comparing the pin number to HIGH.

Something like

Code:
while((millis() - start < 8000UL) && (digitalRead(RX) == HIGH))

Logged

Offline Offline
Sr. Member
****
Karma: 3
Posts: 259
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Delta_G I think that's it I was thinking RX == HIGH is the same as digitalRead(RX) == HIGH
Yep that picked up the start bit of 8mS now I see

Arduino code may be easy if you never coded in Basic I figured where i set pinmode of RX would let me just read RX wrong LOL My code I started with works to after fixing that one thing.
« Last Edit: November 18, 2012, 05:44:03 pm by be80be » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50487
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Arduino code may be easy if you never coded in Basic
Any programming language is easier to learn when you don't have to first unlearn all the bad habits you picked up starting with Basic.
Logged

Pages: [1]   Go Up
Jump to: