Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents

Task 1 Solution

C++

Blocks

Code Block
languagecpp
themeConfluence
int LEDPin = 11;
 
void setup()
{
  pinMode(LEDPin, OUTPUT);
  Serial.begin(9600);

  // just to print out the light reading. Not really necessary
}
 
void loop()
{
  
int delayValue;
  int lightReading = analogRead(A0); //read the analog value
  if(lightReading>512)
  {
    delayValue = 500;
  }
  else
  {
    delayValue = 100;
  }
  Serial.println(lightReading);
  digitalWrite(LEDPin, HIGH);
  delay(delayValue);
  digitalWrite(LEDPin, LOW);
  delay(delayValue);
}
]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:height="400"><ri:attachment ri:filename="image2020-1-21_18-31-1.png" /></ac:image></p><p>Note : The Blocks design shown above is without the Serial print option used in the C++ code on the left</p></div></td></tr></tbody></table><h3>Task 2 Solution</h3><table class="wrapped"><colgroup><col style="width: 574.0px;" /><col style="width: 475.0px;" /></colgroup><tbody><tr><th>C++</th><th>Blocks</th></tr><tr><td><div class="content-wrapper"><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="d702f611-85e4-42bc-8343-92c8ded3fa53"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[
image2020-1-21_18-31-1.pngImage Added

Note : The Blocks design shown above is without the Serial print option used in the C++ code on the left

Task 2 Solution

C++

Blocks

Code Block
int delayValue = 0;
int lightReading = 0;
int brightness = 0;
int buttonState = 0;
int LEDPin = 11;
int lightThreshold = 512;
 
void setup()
{
  pinMode(2, INPUT);
  pinMode(A0, INPUT);
  pinMode(LEDPin, OUTPUT);
  Serial.begin(9600);
}
 
void loop()
{
  buttonState = digitalRead(2);
  if (buttonState == HIGH) {
    lightReading = analogRead(A0);
    if (lightReading > lightThreshold) {
      delayValue = 10;
    } else {
      delayValue = 5;
    }
    for (brightness = 0; brightness <= 255; brightness += 1) {
      analogWrite(LEDPin, brightness);
      delay(delayValue);
    }
    Serial.println("ON");
    
// this will be printed when brightness = 255
    for (brightness = 255; brightness >= 0; brightness -= 1) {
      analogWrite(LEDPin, brightness);
      delay(delayValue);
    }
  } else {
    Serial.println("OFF");
    delay(1000); // printed once every second
    // analogWrite(LEDPin, 0); is not needed here. Why?
  }
}
]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:width="450"><ri:attachment ri:filename="image2020-1-23_0-2-34.png" /></ac:image></p></div></td></tr></tbody></table>

image2020-1-23_0-2-34.pngImage Added