int not needed?

hey im new to this and have just just noticed that in the two following sketches the led is named in the int in one but not the other.
in the first sketch it is not labelled in the int (nor is it called in the loop), and if i do label it in the int section the code runs no different to without it.

const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3;
const int ledPin =  4;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;


void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
  pinMode(buttonPin2, INPUT);     
}



void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
 
  if ( (digitalRead(2) == HIGH  && digitalRead (3) == HIGH) ) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

but if i try to remove the int for the ledstate (which is called in the IF/ELSE) in the following sketch it will not compile.

const int ledPin = 13; // the number of the LED pin

// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}

void loop()
{
// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}

both codes contain an led output and a IF ELSE statement, are they just different ways of doing a very similiar thing?

  1. When you use the "Copy for Forum" in the IDE, do not wrap the code with [code] tags. (I would suggest not using the "Copy For Forum" at all.)

  2. I don't follow your question because in both sketches ledPin is declared as an int.

I think you’re confusing the name of the pin the LED is connected to, and the value written to that pin.

When you say "in the int" do you mean the space outside of the setup, loop and any other declared functions? That's called the global scope.

[quote author=James C4S link=topic=103477.msg776118#msg776118 date=1335621894]

  1. When you use the "Copy for Forum" in the IDE, do not wrap the code with [code] tags. (I would suggest not using the "Copy For Forum" at all.)

  2. I don't follow your question because in both sketches ledPin is declared as an int.

[/quote] in the first code the led is not listed or given a 1/0 state in the section below the const int's, but in the second code it is listed and given a state after being already listed in the const int's. i was under the belief that only inputs were given states?

f111:
in the first code the led is not listed or given a i was under the belief that only inputs were given states?

It depends on what you mean by "state." Variables can be given "states" or better known as values. The variable named "ledState" is a way for the program to keep track of whether the LED should be on or off. The first code directly writes a HIGH (1) or LOW (0) to the pin with the LED. The second code writes the value of ledState.

So yes, these are two ways to do the same thing.

does this mean that because ledState is called in the code it must be given a state in order for the program to know where to start with it? like internal relays in a PLC must be designated NO/NC (software relays) so for the second sketch does it flow like this?

const int ledPin = 13 // assigning pin number

int ledState = LOW // assigning state which the led will start in? (but not needed in first code because not called in loop?)

void setup() { (ledPin,OUTPUT)} // assigning pin as I/O

???

void setup() { (ledPin,OUTPUT)} // assigning pin as I/O

Huh?

Just to be clear, there is no connection between a variable's name and its function in a program. That's up to the programmer to decide. In the 2nd code, you could re-name all of the ledState instances to "bunnies" and the behavior wouldn't change.

That's because variables only hold a value, that's it. So making a call like digitalWrite(13, HIGH) is the same as digitalWrite(ledPin, ledState) assuming that ledPin has been set to 13 and ledState has been set to HIGH.

So in the code you posted with the comments, the phrases "assign pin number" and "assign state", aren't entirely true from the program's perspective. In reality all those statements do is assign a value to a variable.

Side Note:
A convention used by the Arduino libraries are that capital words are marcos. HIGH and LOW is actually converted to the integers 1 and 0, respectively before the code is complied. Turns out that OUTPUT is 1 and INPUT is 0 too. It just makes for easier-to-read code to use meaningful names.

does this mean that because ledState is called in the code

ledState is a variable, not a function; it cannot be "called", only referenced.

MarkT:

void setup() { (ledPin,OUTPUT)} // assigning pin as I/O

Huh?

assigning pin as input or output

No, try again

AWOL:
No, try again

is void setup not where you tell pins that they will be inputs or outputs?? it would appear that way. what is it really?

Do you see a function being called in setup to set the pin mode?

f111:

void setup() { (ledPin,OUTPUT)} // assigning pin as I/O

assigning pin as input or output

Hint: You need to call a function to set the pinMode of a pin...
http://arduino.cc/it/Reference/HomePage

this is getting off track :slight_smile: my original question relates to the following two bits of code.

firstcode
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int ledPin = 4; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status. PLEASE LOOK HERE, why is the ledpin not mentioned ?
int buttonState2 = 0;

second code
const int ledPin = 13; // the number of the LED pin

// Variables will change:
int ledState = LOW; // ledState used to set the LED PLEASE LOOK HERE, but is listed here (this two codes do not run together and are not from the same program)
long previousMillis = 0; // will store last time LED was updated

the led state changes in each different code yet is notlisted as a variable that will change in both???WHY its 1.30AM here im going to die

Why is ledpin not mentioned?
Because it is unrelated to the state of the input.

Go to bed, get some sleep, give your punctuation keys a rest.

And please, use code tags

f111:
the led state changes in each different code yet is notlisted as a variable that will change in both?????????WHY its 1.30AM here im going to die

I already explained:

Just to be clear, there is no connection between a variable's name and its function in a program. That's up to the programmer to decide. In the 2nd code, you could re-name all of the ledState instances to "bunnies" and the behavior wouldn't change.

That's because variables only hold a value, that's it. So making a call like digitalWrite(13, HIGH) is the same as digitalWrite(ledPin, ledState) assuming that ledPin has been set to 13 and ledState has been set to HIGH.

[quote author=James C4S link=topic=103477.msg776234#msg776234 date=1335626589]

f111:

void setup() { (ledPin,OUTPUT)} // assigning pin as I/O

assigning pin as input or output

Hint: You need to call a function to set the pinMode of a pin...
http://arduino.cc/it/Reference/HomePage
[/quote]James im not sure if i follow you? in all the void setups below i have no function calls(i dont know what they are)
i just select whether i want the pin to be an INPUT or OUTPUT. What am i missing?

void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the LED pin as an input:

pinMode(ledPin2, OUTPUT);
pinMode(buttonPin2, INPUT);
}void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}void setup()
{
pinMode(pin, INPUT); //from radio rcvr
pinMode(LEDR, OUTPUT); //tell arduino Red LED is an output
pinMode(LEDG, OUTPUT); //tell arduino Green LED is an output
pinMode(LEDW, OUTPUT); //tell arduino White LED is an output
Serial.begin(9600); //setup serial for debug
}

I think you are misunderstanding what "variables" and "functions" are in the C-Langauge.

In your setup() function you have:

pinMode(ledPin, OUTPUT);    // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT); // initialize the LED pin as an input:
 
  pinMode(ledPin2, OUTPUT);  
  pinMode(buttonPin2, INPUT);

That is 4 function calls to the function "pinMode()". The pinMode() function takes two arguments, both integers. The first is a pin number and the second is an integer representing the direction of that pin (INPUT or OUTPUT).

The other function you are using is "digitalWrite()" inside of the loop() function. digitalWrite() also takes two arguments, both integers. The first is a pin number and the second is either a 1 (HIGH) or 0 (LOW).

This is why both code 1 and code 2 give the same result. Instead of passing a hard number (HIGH) to digitalWrite(), the contents of the variable "ledState" is passed to the function. The code doesn't care where the number comes from.