Lab 17/ HW 17

Lab 17 (Robot w/ Joystick):



Lab 17b (Interrupts):

//Use Hardware-Debounced Switch to Control Interrupt
//Button pins
const int BUTTON_INT = 0; //Interrupt 0 (pin 2 on the Uno)
const int PHOTO_RED = 1;
const int RED =11; //Red LED on pin 11
const int GREEN =10; //Green LED on pin 10
const int BLUE =9; //Blue LED on pin 9
int photo = 0;
//Volatile variables can change inside interrupts
volatile int selectedLED = GREEN;
void setup()
{
pinMode (RED, OUTPUT);
pinMode (GREEN, OUTPUT);
pinMode (BLUE, OUTPUT);

//The pin is inverted, so we want to look at the rising edge
attachInterrupt(BUTTON_INT, swap, RISING);
attachInterrupt(PHOTO_RED, red, FALLING);
}
void swap()
{
//Turn off the current LED
analogWrite(selectedLED, 0);
//Then, choose a new one.
if (selectedLED == GREEN)
selectedLED = RED;
else if (selectedLED == RED)
selectedLED = BLUE;
else if (selectedLED == BLUE)
selectedLED = GREEN;
}
void red()
{
  analogWrite(selectedLED, 0);
  selectedLED = RED;
}
void loop()
{
for (int i = 0; i < 256; i++)
{
  analogWrite(selectedLED, i);
  delay(10);
}
for (int i = 255; i >= 0; i--)
{
analogWrite(selectedLED, i);
delay(10);
}
}




HW 17 ():


Comments

Popular posts from this blog

Lab 13/ HW 13

Lab 21/ HW 21