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
}
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()
}
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 ..
}