need help with writing a program

Hey guys, am having a 'ctr' error not declared in the scope, i need to write a program that runs a continuously for loop (from 0 to 30) that asks the following

If ctr is equal to one print "reading 1
Else If ctr = 3 print reading 3
Else If ctr = 8 print reading 8
Else If ctr = 29 print reading 29

so far this is what i got

[/code// Introduction to if statements
// written by 


// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
   Serial.begin(9600);  
}

// the loop routine runs over and over again forever:
void loop() {
{
  //reading 1
}
else if (ctr == 3)
{
  // reading 3
}
else if (ctr == 8)
{
  // reading 8
}
else if (ctr == 29)
{  
  // reading 29
  
  }

That error is due to you not having declared the variable ctr, as explained for example here

You need a line like:

int ctr;

But then you need to do something to change the value of the counter, otherwise it will never change between all those values you want to test for.

And there's no "if" at the start of your "if".... you need to look at this

Thanks for the quick reply let me try it

You need to define variables where you will store any of your reading or values.

Like jimboZA suggested.

Before any "else if", there is always an "if", curly brackets follows your "ifs".

As for printing on the serial port, you need code lines like

Serial.print(" name of your variables");
Serial.println(variable);

Remember any single expression counts in the programming language.

// ---> used for commenting anything you'd like to : you will observe that with the IDE, changing the line color to light grey, meaning inactivated.

// Any  comments goes here.

taz

See if this does the trick....

void setup() {
  //
  //force 13 led off... having it on by mistake pisses me off
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  
  //start the serial monitor
  Serial.begin(9600);
  
  //here's our 0 to 30 loop
  //it initializes ctr inside the "for"
  
  for (int ctr=1; ctr <= 30; ctr++){
      // print each value as a check that the "for" works
      Serial.println(ctr);    // comment this line out if you don't want it
      
      // and pay special attention to the values 1, 3, 8, 29
      // the || means OR
      if (ctr == 1 || ctr == 3 || ctr == 8 || ctr == 29)
      {
        Serial.print("Reading: ");  // prints "Reading:  " with no line end
        Serial.println(ctr);          // prints value of ctr with a line end
      }
   } 

}

void loop() {
  
  // in this case we only want it to run once to the whole thing is up top in setup()
  
  
}

Hello,

About which jimboza, explained is really nice, printing your readings without using any input. that also me you understand difference between void setup and void loop ..

The for loop concept is introduced,which can be use later for many many applications..

if you did understand this so far, you can try moving with your code with an input this time, like a potentiometer.

an example as u suggested :

// declaration of variables
int ctr;

// declaration of  I/O
const int pot = 1; // potentiometer connected to pin 1 of analog pins on your arduino.

void setup {

Serial.begin (9600); // start serial communication at 9600 bps.

void loop {

int ctr = analogRead(pot); // read value of potentiometer and save it to the variable ctr you created above.

Serial.print (" Pot Read =");
Serial.println(ctr);
// print values from your pot, will be 0 to 1023 wen playing with the pot.

// if statement :

if (ctr >= 1) {

Serial.print ("Reading 1");
}


if (ctr >= 8) {

Serial.print ("Reading 2");
}

// you can add whatever lines you want to print depending on your condition of ctr with the if statement ..

}

cheers ...