Skip to content

11. Output devices

This week I programmed the board I had previously made to move a Servo, I measured it’s power consumption on the group page.

The program

This week’s work was quite simple, I simply created a program which moved a servo back and forth depending on the input comming from a button.

Code

// Max GH 31.03.2022

#include <Servo.h>
Servo popo;

int ang = 0;

const int buttonPin = A6;
const int ledPin =  A7; 

int button = 0;

void setup() {
  // put your setup code here, to run once:
  popo.attach(A2);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  popo.write(30);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  button = digitalRead(buttonPin);
  if (button == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    Serial.println(ang);
    digitalWrite(ledPin, LOW);
    if(ang < 180){
      ang = ang + 30;
      popo.write(ang);
      delay(400);
    }else{
      popo.write(30);
      ang = 30;
    }
  }
}

Last update: April 11, 2022