Right now I have three separate codes, and I have set up my Arduino Uno and the breadboard to run each one (I just have to upload each code separately). The first code is to control a servo with a photoresistor. The second code is to control a servo with a potentiometer. The third code is to control an LED with a button. My goal is to combine the three so that the servo is controlled by the photoresistor unless I push the button. Then it is controlled by the potentiometer. Most of the code was found online. The LED portion of the third code is not necessary, and the third code needs to be modified to change the control of the servo by a button PRESS (i.e. HIGH to LOW or LOW to HIGH) not just holding the button down. Unfortunately, I am inexperienced with coding, so any help you can give me would be greatly appreciated!
#include <Servo.h>
Servo ShutterServo;
int potpin = 0;
int val;
void setup()
{
ShutterServo.attach(12);
}
void loop()
{
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
ShutterServo.write(val);
delay(15);
}
Code Three (button)
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Moderator edit: [code] ... [/code] tags added. (Nick Gammon)
MBEller99:
Right now I have three separate codes, and I have set up my Arduino Uno and the breadboard to run each one (I just have to upload each code separately). The first code is to control a servo with a photoresistor. The second code is to control a servo with a potentiometer. The third code is to control an LED with a button. My goal is to combine the three so that the servo is controlled by the photoresistor unless I push the button. Then it is controlled by the potentiometer. Most of the code was found online. The LED portion of the third code is not necessary, and the third code needs to be modified to change the control of the servo by a button PRESS (i.e. HIGH to LOW or LOW to HIGH) not just holding the button down. Unfortunately, I am inexperienced with coding, so any help you can give me would be greatly appreciated!
#include <Servo.h>
Servo ShutterServo;
int potpin = 0;
int val;
void setup()
{
ShutterServo.attach(12);
}
void loop()
{
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
ShutterServo.write(val);
delay(15);
}
**Code Three (button)**
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Okay that was easier than I thought. The if/else statement worked perfectly. Now my only problem is the button. I can't figure out how to count like you said. Can you point me in the right direction?