sunnuntai 3. kesäkuuta 2012

Our final code

Looks like this will be our final code: (not much different than v0.1)


// Servo Motor
#include <Servo.h>
Servo myservo;
int pos =0;

// VOC thresholds
const int lowthreshold = 300;
const int highthreshold = 350;

// LED pins
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;
int gasSensor = A0; // select input pin for gasSensor
int val = 0; // variable to store the value coming from the sensor


int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

const int DISPLAY_TIME = 200;

void setup()
{
Serial.begin(9600);
myservo.attach(6);
}

void loop()
{
  val = analogRead(gasSensor); // read the value from the pot
  Serial.println( val );
 
  // green LED is on, when the device is on
    if (val < lowthreshold)
    {
     greenIntensity =255;
     blueIntensity = 0;
     redIntensity =0;
     analogWrite(GREEN_LED_PIN,greenIntensity);
     analogWrite(BLUE_LED_PIN,blueIntensity);
     analogWrite(RED_LED_PIN,redIntensity);
    }
 
  if (val > lowthreshold && val < highthreshold)
    {
     greenIntensity =0;
     redIntensity =0;
     blueIntensity = 255;
     analogWrite(GREEN_LED_PIN,greenIntensity);
     analogWrite(BLUE_LED_PIN,blueIntensity);
     analogWrite(RED_LED_PIN,redIntensity);
   
     // get the motor running
     for (pos=0; pos < 180; pos +=1)
     {
       myservo.write(pos);
       delay(15);
     }
     for (pos=180; pos >= 180; pos -=1)
     {
       myservo.write(pos);
       delay(15);
     }
    }
   
     
  if (val > highthreshold)
    {
     greenIntensity =0;
     blueIntensity = 0;
     redIntensity =255;
     analogWrite(GREEN_LED_PIN,greenIntensity);
     analogWrite(BLUE_LED_PIN,blueIntensity);
     analogWrite(RED_LED_PIN,redIntensity);
   
     // get the motor running
     for (pos=0; pos < 180; pos +=1)
     {
       myservo.write(pos);
       delay(5);
     }
     for (pos=180; pos >= 180; pos -=1)
     {
       myservo.write(pos);
       delay(5);
     }
    }
  delay(500);
}

Ei kommentteja:

Lähetä kommentti