Week 4

 
 

This is a simple alarm system that detect the limit of the motor arm angle. The motor is capable of rotating 180 degree, if it is harmful for the motor when it’s operating near its limit, the alarm sets of after it exceeds approx. 176 degree.

 

#include <Servo.h>

Servo myServo;

int const potPin = A1;

int potVal;

int angle;

void setup() {

myServo.attach(2);

Serial.begin(9600);

pinMode(6,OUTPUT);

}

void loop() {

potVal = analogRead(potPin);

Serial.print("potVal: ");

Serial.print(potVal);

angle = map(potVal,0,1023,0,179);

Serial.print(", angle: ");

Serial.print(angle);

myServo.write(angle);

delay(30);

if (potVal>1000) {

tone(6,1000,100);

delay(150);

tone(6,2000,100);

delay(150);

noTone;

}

};

Shijie Zhang