-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino-Start-Template.ino
109 lines (67 loc) · 1.88 KB
/
Arduino-Start-Template.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Servo
//Relay
//Diod
//Sensor in
//Time tracker
//Serial debug
#include <Servo.h>
Servo myservo; // create servo object to control a servo
//Pin assignment
int ledPin = 13;
int servoPin = 9;
int buttonPin = A0;
int sensorPin = A1;
int relayPin = 8;
//Global variables
bool lightIsOn = false;
long timeStamp = 0; //Timestamp for slow actions
//Simple debug function for sending message over serial
void sendDebugVariable(String title,int value){
Serial.print(title);
Serial.print(" : ");
Serial.println(value);
}
// put your setup code in the setup section --------------------------------------------------------------
void setup() {
//Initiate serial communications (USB)
Serial.begin(9600);
//Define pin typess
pinMode(buttonPin,INPUT);
pinMode(sensorPin,INPUT);
pinMode(ledPin,OUTPUT);
pinMode(relayPin,OUTPUT);
//Define servo pin through library
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
}
// put your main code in the loop ----------------------------------------------------------------------------
void loop() {
//Read inputs
int buttonPinValue = analogRead(buttonPin);
int sensor = analogRead(sensorPin);
//Do seom logic on button press
if (buttonPinValue > 900){ //1024 maxh
digitalWrite(relayPin,HIGH);
myservo.write(120);
}
else{
digitalWrite(relayPin,LOW);
myservo.write(60);
}
//Simple slightly inaccurate timer for stuff
if (millis() - timeStamp >= 2000){
if (lightIsOn){
digitalWrite(ledPin,LOW);
lightIsOn = false;
}
else{
digitalWrite(ledPin,HIGH);
lightIsOn = true;
}
timeStamp = millis();
}
//Debug some every loop
sendDebugVariable("Button",buttonPinValue);
sendDebugVariable("Sensor input",sensor);
//sendDebugVariable("Timer",millis()-timeStamp);
delay(200);
}