Pages: [1]   Go Down
Author Topic: Combining Servo + LDR, a Button, and a Potentiometer  (Read 808 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

First Code (photoresistor)
Code:
#include <Servo.h>
Servo ShutterServo;
const int photoresistor = 1;
const int motor = 12;
int photoresistorValue = 0;
int outputValue = 0;
float nice = 0.09;
float myValue;
void setup() {
  Serial.begin(9600);
  ShutterServo.attach(motor);
}
void loop() {
  photoresistorValue = analogRead(photoresistor);
  myValue = float(map(photoresistorValue, 0, 100, 0, 180));
  outputValue += int(myValue - outputValue) *nice;
  Serial.print("sensor = ");
  Serial.print(photoresistorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);
  ShutterServo.write(outputValue);
  delay (10);
}

Code Two (potentiometer)
Code:
#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)
Code:
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)
« Last Edit: November 25, 2012, 01:58:13 am by Nick Gammon » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this before posting a programming question


Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

First Code (photoresistor)
Code:
#include <Servo.h>
Servo ShutterServo;
const int photoresistor = 1;
const int motor = 12;
int photoresistorValue = 0;
int outputValue = 0;
float nice = 0.09;
float myValue;
void setup() {
Serial.begin(9600);
ShutterServo.attach(motor);
}
void loop() {
photoresistorValue = analogRead(photoresistor);
myValue = float(map(photoresistorValue, 0, 100, 0, 180));
outputValue += int(myValue - outputValue) *nice;
Serial.print("sensor = ");
Serial.print(photoresistorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
ShutterServo.write(outputValue);
delay (10);
}

Code Two (potentiometer)
Code:
#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)
Code:
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);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, What is the problem? You know how to read a switch state. You know how to take action based on that state.

You know how to read a potentiometer value and move a servo.

You know how to read an LDR and move a servo.

Do one of these tasks in the if block and the other one in the else block.

When that doesn't work as expected, learn how to detect transitions and to count switch presses.

Quote
Unfortunately, I am inexperienced with coding, so any help you can give me would be greatly appreciated!
You'll never get better if you don't understand the code and try stuff.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I can't figure out how to count like you said.
count++;

Code:
int prevState = LOW;
int currState;
byte count = 0;

void loop()
{
   currState = digitalRead(switchPin); // I really doubt that you have a button attached to the pin
   if(currState != prevState)
   {
      if(currState == HIGH)
         count++;
   }
   prevState = currState;

   if(count >= 2)
     count = 0;

   if(count == 0)
   {
      // Do thing 1
   }
   else
   {
      // Do thing 2
   }
}
Logged

Pages: [1]   Go Up
Jump to: