Code for a Linux + Arduino CPU load monitor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

  1. #include <Servo.h>
  2. Servo myservo; // create servo object to control a servo
  3. // a maximum of eight servo objects can be created
  4. int dial_pos = 0; // variable to store the servo position
  5. int led_pin = 13;
  6. int difference = 0;
  7. unsigned char value1 = 0, value2 = 0;
  8. int value = 0;
  9. void setup()
  10. {
  11. digitalWrite(led_pin, LOW);
  12. Serial.begin(9600); //set serial to 9600 baud rate
  13. myservo.attach(9); // attaches the servo on pin 9 to the servo object
  14. myservo.write(dial_pos);
  15. }
  16. void loop()
  17. {
  18. while(Serial.available() <= 0);
  19. value1 = Serial.read();
  20. while(Serial.available() <= 0);
  21. value2 = Serial.read();
  22. // we're looking to make two bytes into a 16 bit int here
  23. value = value2;
  24. value = value << 8;
  25. value = value | value1;
  26. Serial.println("servo value is:"); // some debugging
  27. Serial.println(value);
  28. // as long as we're above zero, drop off slowly when there's no
  29. // activity
  30. if ((value == dial_pos) && (dial_pos > 0))
  31. value--;
  32. difference = abs(dial_pos - value);
  33. dial_pos = value;
  34. // if there's a big change, blinkenlight
  35. if (difference > 5) {
  36. digitalWrite(led_pin, HIGH);
  37. delay(10 * difference);
  38. digitalWrite(led_pin, LOW);
  39. Serial.flush();
  40. }
  41. for (int i = dial_pos - difference; i <= dial_pos; i++) {
  42. myservo.write(i);
  43. delay(10);
  44. }
  45. }