Courses Fragments

Pcomp Week 3 Assignment

Oscilloscope looks interesting! I want to play with one!

And after that followed the tutorial of TinkderCad, I made a little circuit that I can control the speed, direction and on/off by buttons and potentiometers.

const int controlPin1 = 2;
const int controlPin2 = 3;
const int enablePin = 9;

const int directionSwitchStatePin = 4;
const int onOffSwitchStatePin = 5;

const int potPin = A0;

int onOffSwitchState = 0;
int previousOnOffSwitchState = 0;
int directionSwitchState = 0;
int previousDirectionSwitchState = 0;

int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;

void setup(){
  pinMode(directionSwitchStatePin, INPUT);
  pinMode(onOffSwitchStatePin, INPUT);
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);

void loop(){
  onOffSwitchState = digitalRead(onOffSwitchStatePin);
  directionSwitchState = digitalRead(directionSwitchStatePin);
  motorSpeed = analogRead(potPin)/4;

  if (onOffSwitchState != previousOnOffSwitchState){
    if (onOffSwitchState == HIGH){
      motorEnabled = !motorEnabled;

  if (directionSwitchState != previousDirectionSwitchState){
    if (directionSwitchState == HIGH){
      motorDirection = !motorDirection;

  if(motorDirection == 1){
    digitalWrite(controlPin1, HIGH);
    digitalWrite(controlPin2, LOW);
    digitalWrite(controlPin2, HIGH);
    digitalWrite(controlPin1, LOW);

  if (motorEnabled == 1){
    analogWrite(enablePin, motorSpeed);
  else {
    analogWrite(enablePin, 0);

  previousDirectionSwitchState = directionSwitchState;
  previousOnOffSwitchState = onOffSwitchState;



Of course, I met some trouble when I tweak it. The boolean symbol is “==” instead of “=”. However, I used lots of “=” which causes troubles for me.

And, the quiz:

Explain the difference between an analog sensor and a digital sensor. Give examples of both.

Digital sensors only have two states: on and off. Example: a switch.

In turn, analogy sensors can hold a value in between on and off. Example: a light sensor.

Draw a circuit for connecting a pushbutton as a digital input to a microcontroller. Connect it so that the digital input pin gies HIGH when the button is pushed. What does the resistor (called a pulldown resistor) do?

Pull down the voltage, or the microcontroller would detect voltage from the pushbutton.

Write a program that reads a digital input to an Arduino and turns off an LED whenever the digital input goes LOW. Draw the schematic that goes with this program as well.
int switchPin = 2;
int ledPin = 3;

int switchState = 0;
int ledState = 0;

void setup (){
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);

void loop (){
  switchState = digitalRead(switchPin);
  if (switchState == 0){
    digitalWrite(ledPin, 1);
  else {
    digitalWrite(ledPin, 0);
Write a program that reads a digital input and prints out a message when the input changes from HIGH to LOW.
int previousInputState = 0;
int inputState = 0;

void setup () {
  pinMode(inputPin, INPUT);

void loop () {
  if (inputState !=previousInputState){
    if (digitalRead(inputPin) == LOW) {
      Serial.println("Now is LOW");
  previousInputState = inputState;



Leave a Reply

Your email address will not be published. Required fields are marked *