...
1#include <Servo.h>
2#include <Adafruit_NeoPixel.h>
3
4Servo elbow_servo;
5Servo wrist_servo;
6const int potpin = 0; // analog pin used to connect the potentiometer
7int val; // variable to read the value from the analog pin
8
9//// button cycles through controlling each color
10//// in the neopixel. state 0=stop, 1=red, 2=blue, 3=green, 4=all/white
11const int button_pin = 4;
12int buttonState; // the current reading from the input pin
13int lastButtonState = HIGH; // the previous reading from the input pin
14// the following variables are unsigned longs because the time, measured in
15// milliseconds, will quickly become a bigger number than can be stored in an int.
16unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
17unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
18int led_control_state = 0;
19
20const int pixel_pin = 6;
21const int num_leds = 2;
22Adafruit_NeoPixel pixel = Adafruit_NeoPixel(num_leds, pixel_pin, NEO_GRB + NEO_KHZ800);
23int red = 0;
24int green = 0;
25int blue = 0;
26
27void setup() {
28 Serial.begin(9600);
29 elbow_servo.attach(9); // attaches the servo on pin 9 to the servo object
30 pinMode(button_pin, INPUT_PULLUP); // create a button for the neopixels
31 pixel.begin();
32}
33
34void loop() {
35
36 //// Read signal from variable resistor or potentiometer
37 //
38 val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
39 int v = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
40 elbow_servo.write(v);
41 delay(15); // waits for the servo to get there
42
43
44 //// Toggle between states using a button
45 ////
46 int b = digitalRead(button_pin); // read button
47 // "debounce" the button signal
48 if (b != lastButtonState) lastDebounceTime = millis();
49 if ((millis() - lastDebounceTime) > debounceDelay) {
50 if (b != buttonState) {
51 buttonState = b;
52 if (buttonState == LOW) {
53 led_control_state++;
54 if (led_control_state > 3) led_control_state = 0;
55 }
56 }
57 }
58 lastButtonState = b;
59
60 switch(led_control_state) {
61 case 0:
62 break;
63 case 1:
64 red = 255;
65 blue = 255;
66 green = 0;
67 break;
68 case 2:
69 red = 0;
70 blue = 255;
71 green = 255;
72 break;
73 case 3:
74 red = 255;
75 blue = 0;
76 green = 255;
77 break;
78 }
79
80 Serial.println(led_control_state);
81 // set color ofpixels
82 for(int i=0;i<num_leds;i++){
83 pixel.setPixelColor(i,red,green,blue);
84 pixel.show();
85 delay(15);
86 }
87}
View as plain text