Hello World :),
I started using Arduino about a week ago and I love it. It is quite a challenge for me since I never programmed before but the extensive information on the internet helped me to solve the most issues. Unfortunately I hit a wall. I experiment with controlling Arduino with Processing over the PC and it works quite well, but there is a tiny problem. I would like to use Arduino and Processing to automate parts of my household and to do that I want a servo to switch a lightswitch. I finally found out how to move the servo to a desired degree and it works well but when I combined the servo with an LED and a temperature sensor I apparently made a mistake. I hope you can help me to solve the problem. To switch the lightswitch I want the servo to move back and forth. When I give the command I want the servo to push the switch and then to move backwards to make the switch accessible by hand. The problem is it doesn't stop moving. I tried everything that came to my mind. I tried different and more and more complex if iterations and all kinds of attaching and detaching to make it stop. Then I turned to switchcase ( hint= doesn't work). I was lucky to be able to solve all the problems I had so far but now I hope you can help me.I ran out of ideas. I'm pretty sure that I missunderstood something and hope you can help me. The only problem is the servo, the LED and the temperature sensor work perfectly.
Thank you
#include <Servo.h> //includes servo library
Servo myservo; //creates servo object
int var; //creates variable for switchcase
int ledpin = 13; //defines LED pin
int temppin = 3; //defines temperature sensor pin
int val; //creates variable for temperature sensor data
void setup() {
Serial.begin(9600); //starts serial communication
pinMode(ledpin, OUTPUT); //sets LED pin as output
pinMode(temppin, INPUT); //sets temperature sensor pin as input
myservo.attach(2); //attaches servo
myservo.write(90); } //positions servo to 90 degree
void loop()
{if(Serial.available() > 0) //waits for serial input
var = Serial.read(); //assignes variable to serial input
switch (var)
{
case '3': //executes if input is three
myservo.write(170); //moves servo to 170 degrees
delay(500); //time to allow servo to reach position
myservo.write(90); //moves servo back to 90 degrees
delay(500); //time to allow servo to reach position
break;
case '4': //executes if input is four
myservo.write(30);(2); //moves servo to 30 degrees
delay(500); //time to allow servo to reach position
myservo.write(90); //moves servo back to 90 degrees
delay(500); //time to allow servo to reach position
break;
case '1': //executes if input is three
digitalWrite(ledpin, HIGH); //activates LED
break;
case '2': //executes if input is two
digitalWrite(ledpin, LOW); //deactivates LED
break;
}
{ val = analogRead(temppin); //reads temperature sensor
float voltage = val * 5.0/ 1024;
float temperatureC = (voltage * 1000)/10;
Serial.println(temperatureC);
delay(1000);} //waits for one second
}