I have a windows computer connected to my Arduino Uno. I work in a area with a bunch of cubicles and we leave our desk fairly regularly to go and troubleshoot other computers (I'm a service desk technician). When we leave we are instructed to raise and lower a small flag so management can see whether or not we are sitting at our desk. I've attached my arduino to a servo motor with the following code.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
#define servoPin 3 //~
#define pushButtonPin 2 

int angle =0;    // initial angle  for servo
int angleStep =20;
const int minAngle = 0;
const int maxAngle = 90;

int buttonPushed =0;

void setup() {
  // Servo button demo by Robojax.com
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin);  // attaches the servo on pin 3 to the servo object
   Serial.println("Nathan's Status flag");

void loop() {
  if(digitalRead(pushButtonPin) == LOW){
    buttonPushed = 1;
   if( buttonPushed ){
  // change the angle for next time through the loop:
  angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= minAngle || angle >= maxAngle) {
      angleStep = -angleStep;
       buttonPushed = 0;
    myservo.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");    
  delay(100); // waits for the servo to get there

We also have a policy that we need to keep our computers locked for obvious reasons. I learned my lesson with that early on. I would like to edit my code so that when I lock my computer the flag goes down, then when my computer is unlocked it goes back up. That way as long as I remember to keep my computer locked when I'm away I don't have to worry about the flag. Here is my breadboard/arduino layout. Arduino Layout I also have processing on my computer and am planning on using that. (https://processing.org/)

Any help is greatly appreciated.

  • The aspect of detecting whether or not the computer is currently locked from within Processing is probably better asked on the regular stackexchange. If there's a specific question having to do with the Arduino side running the servo your question could be about that.
    – timemage
    Apr 29, 2021 at 17:22
  • 1
    you could try reversing the sequence of events ... maybe pressing the button at the arduino could lock the computer
    – jsotola
    Apr 29, 2021 at 19:06

1 Answer 1


Read a value through serial (what exactly is entirely up to you) to do the same job the button does now.

Use Serial.read() to get that data, and Serial.available() to know if there is any data to read.

How you generate that value on the PC to send through serial we cannot help you with, as that is outside the scope of this site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.