Sunday, December 15, 2013

Here is another code for servo, LEDS, LCD, temperature sensor, dc motor


#include <Servo.h>
Servo myservo;
int tempIn = 0;       // temperature sensor on Analogue Pin 3
int ledOut = 8;       // LED on Digital Pin 8
int LedOut1 = 10;      // LED on Digital Pin 10
int aiValue = 0;       // input value (0-1023 = 0 to 5v)
float setPoint = 30;    // Trigger value in 'C
float deadband = 0.5; // Differential for reset value in 'C
int potpin = 0;
int val;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
const int buttonPin =  1;
const int MotorPin = 7;
int x=0;
/*
  NOTE: the deadband action can be disabled by setting the deadband to 0
 */
float degC;            // The temperature in Degrees Centigrade
void setup(){
  {
    pinMode(ledOut, OUTPUT);  // Configure the Digital Pin Direction for the LED
    pinMode(LedOut1, OUTPUT);
    lcd.begin(16, 2);
    lcd.clear();
    Serial.begin(9600);
  }
  {
    myservo.attach(9); // attaches the servo on pin 9 to the servo object
  }
  {
    pinMode(buttonPin, INPUT);
    pinMode(MotorPin, OUTPUT);
  }
}
void loop()
{

  degC = getTemperature(getVolts());      //Get the Volts and the Temperature using Helper functions.

  if (degC > setPoint)
  {
    digitalWrite(ledOut, HIGH);      // Temperature Limit Reached, turn the LED on.
    Serial.println(" | Output: ON.");
    lcd.setCursor(0,1);
    lcd.print("  Yellow  ");
    lcd.print(degC);


  }
  else
  {
    if (degC > (setPoint - deadband))
    {
      digitalWrite(ledOut, LOW);       // Temperature dropped below SP and deadband, turn the LED off.
      Serial.println(" | Output: OFF.");
      lcd.print("      ");
    }
  }
  if (degC < setPoint)
  {
    digitalWrite(LedOut1, HIGH);      // Temperature Limit Reached, turn the LED on.
    Serial.println(" | Output: ON.");
    lcd.setCursor(0,1);
    lcd.print("  RED   ");
    lcd.print(degC);
  }
  else
  {
    if (degC < (setPoint - deadband))
    {
      digitalWrite(LedOut1, LOW);       // Temperature dropped below SP and deadband, turn the LED off.
      Serial.println(" | Output: OFF.");
      lcd.print("      ");
    }
  }

  delay(1000);

  {
    val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
    val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
    myservo.write(val); // sets the servo position according to the scaled value
    delay(15); // waits for the servo to get there
  }
  {
    x=digitalRead(buttonPin);
    if(x==HIGH){
      digitalWrite(MotorPin,LOW);
    }
    else{
      digitalWrite(MotorPin,HIGH);
    }
  }
}
/*
Return the voltage for the input value; 0-1023 = 0 to 5V
 */
float getVolts()
{
  int inputValue;
  inputValue = analogRead(tempIn);
  float volts;
  volts = (((float)inputValue / 1024) * 5);

  Serial.print("Input Value: ") ;
  Serial.print(inputValue);
  Serial.print(" | Voltage: ") ;
  Serial.print(volts);
  return volts;
}
/*
Return the temperature in DegreesC for given voltage
 */
float getTemperature(float volts)
{
  float temp = (volts - 0.5) / 0.01 ;

  Serial.print(" | Temperature: ");
  Serial.print(temp);
  Serial.print(" 'C");

  return temp;
}

No comments:

Post a Comment