Laboratory n°2

Hello everyone,
In the program presented below, we will read and write with a serial connection.
In order to realize this, we choose to present a program that will use in our project. It consists of communicating with the serial monitor. We enter the mode (manual or automatic) that we want to use and the intensity of the light we want to put.  The light sensor commutates the value of the light to the Arduino.

Here is the code with the comments :

/* // Regulation of the light //
Reads an analog input pin (it’s a number between 0 – 1024) and next, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor to check the value.

// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 5; // Analog output pin that the LED is attached to
float rawRange = 1024; // 3.3v
float logRange = 5.0; // 3.3v = 10^5 lux
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
int intensity = 0;

int mode = 0;
// Void setup is the function that your arduino call for the initialization
// In this program, we use an analogue reference extern (3.3V) to improve the precision because
// the scale is between 0 – 3.3V for 1024 Values

void setup()
{
analogReference(EXTERNAL);
Serial.begin(9600);
pinMode(analogOutPin, OUTPUT);
}

void loop()
{
// Read the analogic entry
int rawValue = analogRead(analogInPin);
Serial.println(RawToLux(rawValue), DEC);

while (Serial.available() > 0) {

// look for the next valid integer in the incoming serial stream
mode = Serial.parseInt();
// mode = 2 si manuel
// mode = 3 si automatique
intensity = Serial.parseInt();
// The intensity is a number between 0 – 100
// look for the newline. That’s the end of your
// sentence:

if (Serial.read() == ‘\n’)
{
if ( mode ==2 )
{
intensity =  map(intensity, 0, 100, 0 , 255);
analogWrite(analogOutPin, intensity);
// Serial.print(analogOutPin, HEX);
}

if ( mode ==3 )
{
int rawValue = analogRead(analogInPin);

outputValue = map(rawValue, 0, 1023, 255, 0);
analogWrite(analogOutPin, outputValue);
}
}
}
delay(1000);
if (mode == 3)
{//read the analog in value and display this in the serial

int rawValue = analogRead(analogInPin);

outputValue = map(rawValue, 0, 1023, 255, 0);    // the figures 1023 and 255 is for converting 10 bits of the arduino into the 8 bits // It’s ok to use map to convert because we use interger
analogWrite(analogOutPin, outputValue);
}

}
// This function is a little bit strange but this function is supplied with the sensor
float RawToLux(int raw)
{

float logLux = raw * logRange / rawRange;
return pow(10, logLux);
}

You can find the report of the second laboratory in attachment : Laboratory2

See you next week,

Smarthome team

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s