Arduino example from 9am lecture on Wed 2nd Oct 2019

This is the circuit diagram for today’s Arduino example:

This is the breadboard circuit I used for the demonstration during the lecture:

This was the Arduino code as it was at the end of the lecture:

//
// Arduino demonstration program
// Written by Ted Burke
// Written 2-10-2019
//

void setup()
{
  pinMode(2, OUTPUT); // red LED
  pinMode(3, OUTPUT); // green LED
  pinMode(4, OUTPUT); // blue LED
  pinMode(5, OUTPUT); // motor forward
  pinMode(6, OUTPUT); // motor reverse

  Serial.begin(9600); // open serial connection to PC
}

void loop()
{
  int button; // 1 when pressed, 0 when not pressed
  int colour; // 0 for 0V (dark) , 1023 for 5V (bright)

  button = digitalRead(8);
  colour = analogRead(3);

  Serial.println(colour);
  
  if (colour > 512)
  {
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
  }
  else
  {
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
  }
}