Guest User

Untitled

a guest
Jul 31st, 2016
127
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //pin definitions
  2. int redPin = 7;
  3. int greenPin = 5;
  4. int bluePin = 6;
  5. int buttonPin = 2;
  6.  
  7. //serial vars
  8. char commandBuffer[512];
  9. uint8_t buffPos = 0;
  10. byte incomingByte = 0;
  11. String line = "";
  12. bool avail = false;
  13.  
  14. //button vars
  15. int prev = LOW;
  16. int reading = LOW;
  17.  
  18. void setup()
  19. {
  20. Serial.begin(9600);
  21.  
  22. pinMode(redPin, OUTPUT);
  23. pinMode(greenPin, OUTPUT);
  24. pinMode(bluePin, OUTPUT);
  25.  
  26. pinMode(buttonPin, INPUT);
  27.  
  28. analogWrite(redPin, 0);
  29. analogWrite(greenPin, 0);
  30. analogWrite(bluePin, 0);
  31. }
  32.  
  33. void loop()
  34. {
  35.  
  36. SerialRead();
  37.  
  38. if(avail){
  39. avail=false;
  40. if(line.equals("green"))
  41. digitalWrite(greenPin, HIGH);
  42. else if(line.equals("red")){
  43. digitalWrite(redPin, HIGH);
  44. else if(line.equals("yellow"))
  45. digitalWrite(bluePin, HIGH);
  46.  
  47.  
  48. }
  49.  
  50. reading = digitalRead(buttonPin);
  51.  
  52. if(reading!=prev){
  53. if(reading == HIGH)
  54. Serial.println("button_high");
  55. else
  56. Serial.println("button_low");
  57. prev = reading;
  58. }
  59.  
  60.  
  61.  
  62. }
  63.  
  64. void setColor(int red, int green, int blue)
  65. {
  66. analogWrite(redPin, red);
  67. analogWrite(greenPin, green);
  68. analogWrite(bluePin, blue);
  69. }
  70.  
  71. void serialRead(){
  72. if (Serial.available() > 0) {
  73. incomingByte = Serial.read();
  74. if (incomingByte == 13) {
  75.  
  76. line = commandBuffer;
  77. line.trim();
  78. avail = true;
  79.  
  80. memset(commandBuffer, 0, 100);
  81. buffPos = 0;
  82. }else{
  83. commandBuffer[buffPos] = incomingByte;
  84. buffPos++;
  85. }
  86. }
  87. }
RAW Paste Data