perjantai 4. toukokuuta 2012

The code v.0.1

Hello,

We got the first implementation of the project to work.  The sensor is connected to a rgb-led and a servo-motor. Here´s the code:

// 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 pin is on when the device is on
  greenIntensity = 255;
  redIntensity =0;
  blueIntensity = 0;
  analogWrite(GREEN_LED_PIN,greenIntensity);
 
  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);
     }
    }
   
    if (val < lowthreshold)
    {
     greenIntensity =255;
     blueIntensity = 0;
     redIntensity =0;
     analogWrite(GREEN_LED_PIN,greenIntensity);
     analogWrite(BLUE_LED_PIN,blueIntensity);
     analogWrite(RED_LED_PIN,redIntensity);
    }

  delay(500);
/*  for (greenIntensity =0;
       greenIntensity <= 255;
       greenIntensity +=5)
       {
       redIntensity = 255-greenIntensity;
       analogWrite(GREEN_LED_PIN,greenIntensity);
       analogWrite(RED_LED_PIN,redIntensity);
       delay(DISPLAY_TIME);
       }
   for (blueIntensity =0;
       blueIntensity <= 255;
       blueIntensity +=5)
       {
       greenIntensity = 255-blueIntensity;
       analogWrite(BLUE_LED_PIN,blueIntensity);
       analogWrite(GREEN_LED_PIN,greenIntensity);
       delay(DISPLAY_TIME);
       } */
      
}

We have also found a cardboard box for the future testing purposes.

Ei kommentteja:

Lähetä kommentti