Hi,
I'm trying to create a program to when I push a button it interrupts and changes which servo is receiving the distance sensor value. I need to use a interrupt because I'm wanting the servos to receive a "live" feed from the distance sensor.
Here is Jeremy Blum's Code (Tutorial 10 for Arduino: Interrupts + Debouncing – JeremyBlum.com):
//Program by Jeremy Blum
//www.jeremyblum.com
//Use hardware Debounced Switch to Control Interrupt
//Setup the Button
int buttonInt = 0; //AKA digital pin 2
//Setup the LEDs
int yellowLED = 11;
int redLED = 10;
int greenLED = 9;
int nullLED = 6;
volatile int selectedLED = greenLED;
//Setup the Distance Sensor
int distPin = 0;
void setup()
{
pinMode (redLED, OUTPUT);
pinMode (greenLED, OUTPUT);
pinMode (yellowLED, OUTPUT);
pinMode (nullLED, OUTPUT);
attachInterrupt(buttonInt, swap, RISING);
}
void swap()
{
if (selectedLED == greenLED)
selectedLED = redLED;
else if (selectedLED == redLED)
selectedLED = yellowLED;
else if (selectedLED == yellowLED)
selectedLED = nullLED;
else
selectedLED = greenLED;
}
void loop()
{
//Read Distance Sensor
int dist = analogRead(distPin);
int brightness = map(dist, 0, 1023, 0, 255);
//Control LED Brightness
analogWrite(selectedLED, brightness);
}
I was thinking of changing the led's to servos:
void swap()
{
if (selectedServo == servoPin)
selectedServo = servoExample;
else (selectedServo == servoExample)
selectedServo = servoPin;
}
Except this does not work.
How can I achieve this?