serial_led_minecraft.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <Adafruit_NeoPixel.h>
  2. #define PIN 9
  3. #define NUMPIXELS 3
  4. int flag=0;
  5. int in_error=0;
  6. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  7. int incomingByte; // a variable to read incoming serial data into
  8. void setup() {
  9. // initialize serial communication:
  10. Serial.begin(9600);
  11. // initialize the LED pin as an output:
  12. pixels.begin(); // This initializes the NeoPixel library.
  13. pixels.setPixelColor(0, pixels.Color(8,9,10)); // Moderately bright green color.
  14. pixels.show(); // This sends the updated pixel color to the hardware.
  15. delay (100);
  16. pixels.setPixelColor(0, pixels.Color(0,0,0)); // Moderately bright green color.
  17. pixels.show();
  18. }
  19. void loop() {
  20. // see if there's incoming serial data:
  21. if (Serial.available() > 0) {
  22. // read the oldest byte in the serial buffer:
  23. incomingByte = Serial.read();
  24. Serial.println(incomingByte);
  25. switch (incomingByte) {
  26. case 48: //0 = off
  27. off_();
  28. break;
  29. case 84: //T
  30. tristan();
  31. break;
  32. case 86: //V
  33. valentin();
  34. break;
  35. case 69: //E
  36. ethan();
  37. break;
  38. case 50: //2
  39. two();
  40. break;
  41. case 51: //3
  42. three();
  43. break;
  44. case 88: //x
  45. in_error=1;
  46. break;
  47. }
  48. }
  49. if ( in_error == 1)
  50. {
  51. if (flag == 0){
  52. pixels.setPixelColor(0, pixels.Color(20,0,0)); // Moderately bright green color.
  53. pixels.setPixelColor(1, pixels.Color(20,0,0)); // Moderately bright green color.
  54. pixels.setPixelColor(2, pixels.Color(20,0,0)); // Moderately bright green color.
  55. pixels.show(); // This sends the updated pixel color to the hardware.
  56. flag=1 ;
  57. delay (100);
  58. }
  59. if (flag == 1){
  60. pixels.setPixelColor(0, pixels.Color(0,0,0)); // Moderately bright green color.
  61. pixels.setPixelColor(1, pixels.Color(0,0,0)); // Moderately bright green color.
  62. pixels.setPixelColor(2, pixels.Color(0,0,0)); // Moderately bright green color.
  63. pixels.show(); // This sends the updated pixel color to the hardware.
  64. flag=0 ;
  65. delay (300);
  66. }
  67. }
  68. }