For loop

Hello,

I am hoping someone can point out where I am making my mistake here.

int analogPin = 0;
int val = 0;

void setup()
{
    Serial.begin(9600);
    pinMode(analogPin, INPUT)
}

void loop
{
    val = analogRead(analogPin);
    if(val < 200)
    {
        Serial.println("No pressure");
        delay(1000)
    }
    if(val > 200)
    {
        for(int x=0; x<2; x++)
        {
            Serial.print("Pressure");
            Serial.print("-------------------");
            Serial.print("The pressure applied is: ");
            Serial.print("val");
            delay(1000);
        }
    }
}

Basically, I want the code in the for loop to be display twice, but it keeps repeating a load of times, and I cannot see where I am going wrong.

Thanks in advance.

Seán

You do realise that loop() is being invoked over and over, right? How can you tell that your inside loop is not repeating the correct number of times?

Gardner beat me to it :slight_smile:

Basically, for as long as val > 200, the print will run every second.

Right, yes, of course!! I'm an idiot.

Thanks!

Seán

I have another question. can I just remove the loop() function, or am I able to just run the loop once?

Thanks

Seán

You have to have a "loop", but it can be empty.

val also shouldn't be in quotes in the serial print...

Oh yea, that's just my typo from retyping the program into the post.

Oh yea, that's just my typo from retyping the program into the post

You don't have to do that - cut-and-paste works.

Normally I do, but I was programming on one computer and online on a different one, and it was just easier to retype it out again.

Seán

Although it may be overkill for your code, one simple way to limit how often something happens is a counter or flag - and making it static means it will continue valid through every pass of loop():

static int repeatCount=3;
if (repeatCount>0)
{
  repeatCount=repeatCount-1; // or shorten to --repeatCount;
// do something here
}

That section will only be run three times - and that's it.