Tuesday, September 22, 2009

Playing with IF statements

Had a lot of fun today with IF statements and derivatives...Here is some code and video from my experimentation today.

This code turns on and off 3 LED's depending on the amount of force sensed by a Force Sensitive Resistor.

////////////////////
///////////////////
int potPin = 0;
int ledPin = 3;
int ledPinA = 5;
int ledPinB = 6;
int potValue = 0;

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
}

void loop()
{
potValue = analogRead(potPin);

Serial.println(potValue);


if (potValue >= 600) {
analogWrite(ledPin, potValue / 4);
}
else analogWrite(ledPin, 0);

if (potValue < 600 && potValue > 500) {
analogWrite(ledPinA, potValue / 4);
}
else analogWrite(ledPinA, 0);

if (potValue <= 500 && potValue > 200) {
analogWrite(ledPinB, potValue / 4);
}
else analogWrite(ledPinB, 0);

delay(10);
}



_______________________________________________

The following code allowed for the use of two sensors and included some OR (||) statements.
////////////////////////////
///////////////////////////
int potPin = 0;
int potFlex = 1;
int ledPin = 3;
int ledPinA = 5;
int ledPinB = 6;
int potValue = 0;
int potFlexValue = 0;

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
}

void loop()
{
potValue = analogRead(potPin);
potFlexValue = analogRead(potFlex);

Serial.println(potFlexValue);


if (potValue >= 600 || potFlexValue < 450) {
analogWrite(ledPin, potValue / 4);
analogWrite(ledPin, potFlexValue / 4);
}
else analogWrite(ledPin, 0);

if (potValue < 600 && potValue > 500) {
analogWrite(ledPinA, potValue / 4);
}
else analogWrite(ledPinA, 0);

if (potValue <= 500 && potValue > 200) {
analogWrite(ledPinB, potValue / 4);
}
else analogWrite(ledPinB, 0);

delay(10);

}



_____________________________________________________

In this last snippet of code is a simple application of the "Hello World!" program using a sensor to tell a user if they are "strong" or not.

/////////////////////////////////
////////////////////////////////
int potPin = 0;
int potValue = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
potValue = analogRead(potPin);


if (potValue >= 800) {
Serial.println("You are Strong");
}
else Serial.println("You are weak");

delay(10);
}



________________________________________________________

A lot of fun with this stuff. Really starting to see the giant world of possibility when combining the physical world with instructions.