ArduinoGeneral

Beetle trouble when using Rx as interrupt

userHead GlowGuy 2014-09-06 08:06:36 4644 Views3 Replies
Hi. I am using a couple of Beetles to run some LED strips. I have my 5v and gnd connected. I connected a data in line to D9. I started using a momentary button on A0 to switch between patterns for the LEDs. I was getting some issues with reading the button during the code as it would have to cycle between the pattern and catch the button read between cycles. I decided to switch the button wire from A0 to Rx and use an interrupt since Rx corresponds to pin 0 and according to this [url=http://arduino.cc/en/Reference/AttachInterrupt]http://arduino.cc/en/Reference/AttachInterrupt[/url] that is an interrupt on the Leonardo.
When I did this and uploaded the new code with the interrupt, after the upload seemed to be through, my computer no longer recognized the beetle as a Leonardo.
I thought initially that it corrupted the boot loader. I tried it on another beetle and the same thing happened. I rewired in a third beetle back to the original configuration and original code and got it back to working.

Now I have tried just plugging in the original beetle and the computer does not recognize it (Windows 7). The second beetle is recognized but I'm not sure if the code is getting uploaded. Is there a simple code like BLINK to test the beetle with?

THanks for any help.
2014-09-11 11:42:40 It seems that the Leonardo Serial port has be occupied, and it is locked now.
There is a recovery way I tried before. Maybe you can have a try.

Step1?
There are 6 pads on the back of the module. This is ISCP interface. Please plug beetle to your usb cable. and watch your device manager. Using a cable to touch Pin 5 and Pin 6, Beetle will reset. and Device manager should appear a COM port named "Arduino Leonardo bootloader (COM x)". Then it will disappear. If you see this info. you could go Step2.

Step2:
Open your arduino IDE, and open any sketch like "Blink" select the board to "Leonardo". Now there is no COM port now. Click "Upload", after IDE has completed verifying, touch the Pin 5 and Pin 6 immediately. It need to be try several times, because the time is not easy to be caught.
userHeadPic Grey.CC
2014-09-10 21:07:18 This is the code that I uploaded that caused the Beetle to longer be recognized.
[code]#include <Adafruit_NeoPixel.h>

#define PIN 9

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//  NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//  NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//  NEO_GRB    Pixels are wired for GRB bitstream (most NeoPixel products)
//  NEO_RGB    Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a dfrobot circuit...if you must, connect GND first.


//added by Damian for cycling states based on button pushes
const int BUTTON = 0;  //the pin the button is connected to

volatile int buttonCyclePosition = 0; //the position we are at in our list
const int buttonMaxPosition = 5;  //the number of items in our list (-1 as we start at 0)
volatile int lastButtonState = 0;    //previous state of the button
volatile int buttonState = 0;        //current state of the button

//end variables for button cycling


void setup() {
  //setup for button cycling
  pinMode(BUTTON, INPUT);
  attachInterrupt (2, buttonChange, LOW);
  //end addition for button cycling
 
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  strip.setBrightness(128);
}

void loop() {
//  //read current pushbutton state
//  buttonState = digitalRead(BUTTON);
// 
//  //compare to previous state
//  if ( buttonState != lastButtonState ) {
//    //if button state has changed, increment the cycle position
//    buttonCyclePosition++;
//    delay (500);
//    //if this takes us beyond the maximum boundary of the cycle, reset position to 0
//    if ( buttonCyclePosition > buttonMaxPosition ) {
//      buttonCyclePosition = 0;
//      delay (500);
//    }
//    //record current state of the button as the previous state for the next comparison
//    lastButtonState = buttonState;
//  }
 
  switch(buttonCyclePosition) {
    case 0:
      colorWipeDemo();
      break;
    case 1:
      simpleTheaterChaseDemo();
      break;
    case 2:
      rainbow(20);
      break;
    case 3:
      rainbowCycle(20);
      break;
    case 4:
      theaterChaseRainbow(50);
      break;
    case 5:
      originalJJDemo();
      break;
  }
}

//button interrupt to change pattern
void buttonChange() {
  //read current pushbutton state
  buttonState = digitalRead(BUTTON);
 
  //compare to previous state
  if ( buttonState != lastButtonState ) {
    //if button state has changed, increment the cycle position
    buttonCyclePosition++;
    delay (100);
    //if this takes us beyond the maximum boundary of the cycle, reset position to 0
    if ( buttonCyclePosition > buttonMaxPosition ) {
      buttonCyclePosition = 0;
      delay (100);
    }
    //record current state of the button as the previous state for the next comparison
    lastButtonState = buttonState;
  }
    switch(buttonCyclePosition) {
    case 0:
      colorWipeDemo();
      break;
    case 1:
      simpleTheaterChaseDemo();
      break;
    case 2:
      rainbow(20);
      break;
    case 3:
      rainbowCycle(20);
      break;
    case 4:
      theaterChaseRainbow(50);
      break;
    case 5:
      originalJJDemo();
      break;
  }
}

//functions added for using a button to cycle poi patterns
void colorWipeDemo() {
  colorWipe(strip.Color(255, 0, 0), 50); // Red
  colorWipe(strip.Color(0, 255, 0), 50); // Green
  colorWipe(strip.Color(0, 0, 255), 50); // Blue
}

void simpleTheaterChaseDemo(){
  theaterChase(strip.Color(127, 127, 127), 50); // White
  theaterChase(strip.Color(127,  0,  0), 50); // Red
  theaterChase(strip.Color(  0,  0, 127), 50); // Blue
}

void originalJJDemo(){
  // Some example procedures showing how to display to the pixels:
  colorWipe(strip.Color(255, 0, 0), 50); // Red
  colorWipe(strip.Color(0, 255, 0), 50); // Green
  colorWipe(strip.Color(0, 0, 255), 50); // Blue
  // Send a theater pixel chase in...
  theaterChase(strip.Color(127, 127, 127), 50); // White
  theaterChase(strip.Color(127,  0,  0), 50); // Red
  theaterChase(strip.Color(  0,  0, 127), 50); // Blue

  rainbow(20);
  rainbowCycle(20);
  theaterChaseRainbow(50);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();
   
      delay(wait);
   
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {    // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
        for (int i=0; i < strip.numPixels(); i=i+3) {
          strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
        }
        strip.show();
     
        delay(wait);
     
        for (int i=0; i < strip.numPixels(); i=i+3) {
          strip.setPixelColor(i+q, 0);        //turn every third pixel off
        }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
  WheelPos -= 85;
  return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
  WheelPos -= 170;
  return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}
[/code]
userHeadPic GlowGuy
2014-09-09 18:45:41 Hello,

Are you using the Arduino sample code?
This one:
[code]int pin = 13;
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, blink, CHANGE);
}

void loop()
{
  digitalWrite(pin, state);
}

void blink()
{
  state = !state;
}[/code]

Are you using INT2 and INT3?
userHeadPic Grey.CC