piątek, 21 marca 2014

Zabawa RGB z telefonem w systemie android

Projekt przygotowany przez Bee Project (Thailand)
Aplikacja na androida

kod:

//przypisujemy piny do RGB
const int redPin = 6;
const int greenPin = 5;
const int bluePin = 3;

#define REDPIN 6
#define GREENPIN 5
#define BLUEPIN 3

#define FADESPEED 5

void setup() {
// initialize serial:
Serial.begin(9600);

// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

Serial.print("Arduino control RGB LEDs Connected OK ( Sent From Arduinno Board )");
Serial.print('\n');
}

void loop() {

// if there's any serial available, read it:
while (Serial.available() > 0) {

 // look for the next valid integer in the incoming serial stream:
 int red = Serial.parseInt();
 // do it again:
 int green = Serial.parseInt();
 // do it again:
 int blue = Serial.parseInt();

 // look for the newline. That's the end of your
 // sentence:
 if (Serial.read() == '\n') {
          
   // constrain the values to 0 - 255 and invert
   // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"

   red = constrain(red, 0, 255);
   green = constrain(green, 0, 255);
   blue = constrain(blue, 0, 255);
 
   // fade the red, green, and blue legs of the LED:
   analogWrite(redPin, red);
   analogWrite(greenPin, green);
   analogWrite(bluePin, blue);
 
  // print the three numbers in one string as hexadecimal:
  Serial.print("Data Response : ");
  Serial.print(red, HEX);
  Serial.print(green, HEX);
  Serial.println(blue, HEX);
 }
}

}

Brak komentarzy:

Prześlij komentarz