Tracking the state of pin 12

I'm using an Adruino UNO board for my Introduction to Engineering class.
Our assignment is posted as an attachment to this.

My problem is the second part. How am I supposed to make it so that when there is voltage being sent to pin 12, it will make the LED connected to pin 13 light up?
This is my program so far:

// Pin 13 becomes known as LED since the LED is plugged into this pin
int LED = 13;
// The setup function begins once the baord is powered or reset is pressed
void setup() {
// initialize digital the LED as an output
pinMode(LED, OUTPUT);
// initialize pin 12 as an input
pinMode(12, INPUT);
// initialize serial port at 9600 bits-per-second baud rate
Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
// The value at pin 12 is read
digitalRead(12);
if(
digitalWrite(LED, HIGH); // The LED is turned on for 4 time units
delay(2000);
else(
digitalWrite(LED, LOW); // The LED is turned off for 2 time units
delay(1000);
digitalWrite(LED, HIGH); // The LED is turned on for 4 time units
delay(2000);
digitalWrite(LED, LOW); // The LED is turned off for 4 time units
delay(1000);
}

Lab_2_MicrocontrollerGPIO (1).docx (79.9 KB)

You might want to pay just a little more attention in class, just a tiny bit.

digitalRead(12);
if(
digitalWrite(LED, HIGH); // The LED is turned on for 4 time units
delay(2000);
else(

if( /*condition */)
{
  // something done if true
}
else 
{
  // something done if false
}

4 time units

Whats a time unit?

I do pay attention in class. We've been taught how to make an LED blink, but that night we were given an assignment to blink our initials in morse code, as well as use the serial monitor (which we had never used) to send ascii art to the computer through a com port. This time we are forced to something else we weren't taught. My classmates will definitely thank you as much as I do. This is my prof's first year teaching.

We were told to assume that having delay(500) is equal to "one time unit"

then you can do this...

int timeUnit = 500;  //milliseconds

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  digitalWrite(13, HIGH);
  delay( 4 * timeUnit);
  digitalWrite(13, LOW);
  delay(timeUnit);
}

I've changed my loop function to this. I am getting the error "
ArduinoGPIO.ino: In function 'void loop()':
ArduinoGPIO:17: error: 'else' without a previous 'if'
ArduinoGPIO:17: error: lvalue required as left operand of assignment"

// the loop function runs over and over again forever
void loop() {
// The value at pin 12 is read
if(digitalRead(12)>0);
digitalWrite(LED, HIGH); // The LED is turned on if there is voltage being sent to pin 12
else(digitalRead(12)=0);
digitalWrite(LED, LOW); // The LED is turned off if there is no voltage being sent to pin 12

}

BulldogLowell:
then you can do this...

int timeUnit = 500;  //milliseconds

[/quote]

I appreciate the help with making the code more simple!

yes....

read reply #1....

if (somethingIsTrue){
  do this;
}
else {  // that something ended up not true
  do that;
}

pay close attention to the location of the curly braces and the semicolons

I tried that earlier and was given another error

ArduinoGPIO.ino: In function 'void loop()':
ArduinoGPIO:18: error: lvalue required as left operand of assignment
ArduinoGPIO:18: error: expected `;' before '{' token

I have no idea what "Ivalue" means

// the loop function runs over and over again forever
void loop() {
  // The value at pin 12 is read
  if(digitalRead(12)>0){
  digitalWrite(LED, HIGH); // The LED is turned on if there is voltage being sent to pin 12
  }
  else(digitalRead(12)=0){
  digitalWrite(LED, LOW);  // The LED is turned off if there is no voltage being sent to pin 12
  }
}

yes, except digitalRead() will return only a one or a zero... so programmers write your block like this:

void loop() {
  if(digitalRead(12) = LOW){
  digitalWrite(LED, HIGH); 
  }
  else // since the answer is zero or one... the second expression is redundant...
{
  digitalWrite(LED, LOW); 
  }
}

and we like to use the keywords HIGH and LOW will be recognized by arduino's editor and highlighted in your code!

doubtfulalpha:
I've changed my loop function to this. I am getting the error "
ArduinoGPIO.ino: In function 'void loop()':
ArduinoGPIO:17: error: 'else' without a previous 'if'
ArduinoGPIO:17: error: lvalue required as left operand of assignment"

// the loop function runs over and over again forever
void loop() {
// The value at pin 12 is read
if(digitalRead(12)>0);
digitalWrite(LED, HIGH); // The LED is turned on if there is voltage being sent to pin 12
else(digitalRead(12)=0);
digitalWrite(LED, LOW); // The LED is turned off if there is no voltage being sent to pin 12

}

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

How to use this forum


  else(digitalRead(12)=0){

Huh? Where is the "if"?

And why the "="?

doubtfulalpha:
This time we are forced to something else we weren't taught.

That is incredibly sad. Fortunately, Google still works.

Got it!
I understand now. It may be redundant, but this is my 2nd week programming, and so I want to make sure that it's all spelled out in black and white (and orange and blue) until I know the simple ins and outs. But I'll remember that for sure!

BulldogLowell:
yes, except digitalRead() will return only a one or a zero... so programmers write your block like this:

void loop() {

if(digitalRead(12) = LOW){

}

. . .and the programmers who want to remain in employment write it like this if(digitalRead(12) == LOW), thereby neatly avoiding the lvalue error message.

hehe...

:cold_sweat:

There are lvalues and rvalues. The easiest way to think of them is with respect to the equal sign. Some things can be on the left of an equal sign. Some things can't.

int r = digitalRead(4);

r is an lvalue. It is on the left of an equal sign, and can have a value assigned to it.

if(digitalRead(4) = LOW)

digitalRead(4) is on the left of the equal sign, but is NOT an lvalue. You can not assign a value to a function call. digitalRead(4) is an rvalue, meaning that it can only appear on the right side of an equal sign.

Of course,

if(LOW = digitalRead(4))

is wrong, too, because LOW isn't an lvalue either. It's a numeric value, 0, so it can't have a new value assigned to it.

BulldogLowell:
yes, except digitalRead() will return only a one or a zero... so programmers write your block like this:

void loop() {

if(digitalRead(12) = LOW){
 digitalWrite(LED, HIGH);
 }
 else // since the answer is zero or one... the second expression is redundant...
{
 digitalWrite(LED, LOW);
 }
}




and we like to use the keywords HIGH and LOW will be recognized by arduino's editor and highlighted in your code!

And it can all be done in one line of code: digitalWrite(LED, !(digitalRead(12)));