Back to Parent

#include<math.h>

// name the pins
int redPin = A6; //Red
int greenPin = A5; //Green
int bluePin = A4; //Blue
int buttonPin = D2; //Switch
int forcePin = A7; //Force Sensor
int photoPin = A0; //Photo Resistor
int ledPin1 = D6; //indicator pin 1
int ledPin2 = D5; //indicator pin 2
int ledPin3 = D4; //indicator pin 3

int redValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int greenValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int blueValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255

int forceValue; //define variable to store force sensor reading
int photoValue; //define variable to store photo sensor reading

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

// This routine runs only once upon reset
void setup()
{
   Spark.variable("light", &photoValue, INT); //creates online API to read variables
   Spark.variable("force", &forceValue, INT);

   // Configure the pins to be outputs
   pinMode(redPin, OUTPUT);
   pinMode(greenPin, OUTPUT);
   pinMode(bluePin, OUTPUT);
   pinMode(ledPin1, OUTPUT);
   pinMode(ledPin2, OUTPUT);
   pinMode(ledPin3, OUTPUT);

   pinMode(buttonPin, INPUT);
   pinMode(forcePin, INPUT);
   pinMode(photoPin, INPUT);

   // Initialize all the LEDs to be OFF
   analogWrite(redPin, redValue);
   analogWrite(greenPin, greenValue);
   analogWrite(bluePin, blueValue);

   //make the indicator LED's blink once to show the system is on
   digitalWrite(ledPin1, LOW);
   digitalWrite(ledPin2, LOW);
   digitalWrite(ledPin3, LOW);
   delay(500);
   digitalWrite(ledPin1, HIGH);
   digitalWrite(ledPin2, HIGH);
   digitalWrite(ledPin3, HIGH);
   delay(500);
   digitalWrite(ledPin1, LOW);
   digitalWrite(ledPin2, LOW);
   digitalWrite(ledPin3, LOW);
   delay(1500);
}



void loop()
{
  forceValue = analogRead(forcePin); //read from the force sensor
  photoValue = analogRead(photoPin); //read from the photoresistor
  buttonState = digitalRead(buttonPin); // read from the switch



  if (forceValue <500)
  {
    analogWrite(redPin, 255);
    analogWrite(bluePin, 255);
    analogWrite(greenPin, 255);

    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    delay(600);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
    delay(600);

  }
  else
  {
    if (buttonState == LOW)
    {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, HIGH);

      analogWrite(redPin, 255);

      float in, out;

      for (in = 0; in < 6.283; in = in + 0.0001)
        {
          out = sin (in) * 127.5 + 127.5;
          analogWrite(bluePin, out);
          analogWrite(greenPin, out);
        }
      }
      else
      {

    //for the part where the RGB goes up as the photoresistor goes down
        int redValue = map(photoValue,0, 4095, 0, 255);
        int greenValue = map(photoValue,0, 4095, 0, 255);
        int blueValue = map(photoValue,0, 4095, 0, 255);

        analogWrite( redPin, redValue);
        analogWrite( greenPin, greenValue);
        analogWrite( bluePin, blueValue);

        if(photoValue < 2000)
        {
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, LOW);
          digitalWrite(ledPin3, LOW);
        }

        else if(photoValue < 3000)
        {
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, HIGH);
          digitalWrite(ledPin3, LOW);
        }

        else
        {
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, HIGH);
          digitalWrite(ledPin3, HIGH);
        }

      }
  }

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0