Flora Brakelight Backpack

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
kkolbo
 
Posts: 68
Joined: Fri Mar 22, 2013 3:00 pm

Re: Flora Brakelight Backpack

Post by kkolbo »

watsonthomasb wrote:Do you guys know what portion of the code controls the brightness level of the Pixels? I've been looking over the code but can't find it and I don't know if it's buried in the Pixel Library some where.

Thanks!
The brightness of the pixel is controlled by the values sent for color. For example. RED = 255, 0, 0 DIM RED 140, 0, 0. The same is true with mixed colors. Use lower values for lower brightness.

KK

alan1408
 
Posts: 7
Joined: Tue Feb 18, 2014 1:33 pm

Re: Flora Brakelight Backpack

Post by alan1408 »

I have a problem with the code supplied with the Brakelight Backpack Project. I'm using Arduino UNO instead of Flora Board. I set up my code as follows:

int dataPin = 2;
int clockPin = 3;
const int cPin = 11;
const int dPin = 6;

The turn signals seem to work properly but the brakelight is not working when my bike decelerates. Is it okay to use UNO instead of Flora for this project? I'm planning to add an Ultrasonic sensor as well onto my backpack. The motion sensor reacts as the indicator for the user whenever vehicles come from behind. I'm new with coding and Arduino. Sorry for my English though ;)

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Flora Brakelight Backpack

Post by adafruit_support_bill »

The project should work with the Uno. But you need to use the UNO SCL and SDA pins to connect the LSM303.
https://learn.adafruit.com/lsm303-accel ... and-wiring

alan1408
 
Posts: 7
Joined: Tue Feb 18, 2014 1:33 pm

Re: Flora Brakelight Backpack

Post by alan1408 »

Thanks, Bill.

I have another question. How do I know that the Flora accelerometer is functioning properly? Do I need any method to analyze the performance of the accelerometer?

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Flora Brakelight Backpack

Post by adafruit_support_bill »

If you are using the Flora version of the accelerometer, you need to power it with 3.3v. Since it is designed to connect with a 3.3v Flora, it does not have an on-board regulator. You can use the 3.3v pin on the UNO for power.

To test, there is an example sketch in the LSM303 library.

alan1408
 
Posts: 7
Joined: Tue Feb 18, 2014 1:33 pm

Re: Flora Brakelight Backpack

Post by alan1408 »

Hi, Bill

I tried to combine the ultrasonic code and the Flora backpack code. Unfortunately, the accelerometer sensor is not working properly. Any ideas how to fix this? Here is my code.

Code: Select all

#include <Wire.h>
#include <Adafruit_LSM303.h>
#include "SPI.h"
#include "Adafruit_WS2801.h"

Adafruit_LSM303 lsm;

#define BRAKETHRESHOLD        350
#define BRAKETIMETHRESHOLD    200

int dataPin  = 2;    // Yellow wire on Adafruit Pixels
int clockPin = 3;    // Green wire on Adafruit Pixels
const int cPin = 11;
const int dPin = 6;

// pin connected to analog output on maxsonar sensor
int sonarPin = A0;
// pin connected to digital output on red led
int ledPin = 7;


// Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
// The LED strips are 32 LEDs per meter but you can extend/cut the strip
Adafruit_WS2801 strip = Adafruit_WS2801(36,dataPin,clockPin);

int start = 0;

int prevX = 0;
int currentX = 0;

int cState = 0;
int dState = 0;

long brakeTime = 0;

void setup() 
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  // Start up the LED strip
  strip.begin();
  // Update the strip, to start they are all 'off'
  strip.show();
  // Try to initialise and warn if we couldn't detect the chip
  if (!lsm.begin())
  {
    Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!");
    while (1);
  }
  pinMode(cPin, INPUT); 
  pinMode(dPin, INPUT);
}

void loop()
{
  // The Arduino’s analog-to-digital converter (ADC) has a range of 1024,
  // which means each bit is ~4.9mV.
  // Therefore, to convert the number returned by the ADC to inches, we
  // have to divide by 2.
  // To convert to cm is necessary to divide by 0.3937.
  // 1 centimeter = 0.3937 inch
  int distance = analogRead(sonarPin)/(4*0.3937);

  Serial.print(distance, DEC); // prints the value read
  Serial.println("cm");
  // If something is at a distance less than 20 cm than LED lights up.
  if(distance < 300) { 
    digitalWrite(ledPin, HIGH);
    delay(5);
    //digitalWrite(ledPin, LOW);
    //delayMicroseconds(500);
  }
  else
  digitalWrite(ledPin, LOW);
  delay(5);

{
  check_switches();      // when we check the switches we'll get the current state
  
  lsm.read();
  currentX = abs(lsm.accelData.x);
  
  if (start == 0){
    prevX = currentX;
    start = 1;
  }
  
  int i = currentX - prevX;

  if (abs(i) > BRAKETHRESHOLD) {
    brakeTime = millis();
    int strikes = 0;
    while ((abs(i) > BRAKETHRESHOLD) && (strikes < 3)) {
      if (abs(i) < BRAKETHRESHOLD) {
        strikes = strikes + 1; 
      }
      lsm.read();
      currentX = abs(lsm.accelData.x);
      i = currentX - prevX;
      
      if ((millis() - brakeTime) > BRAKETIMETHRESHOLD) {
        brakeLights(Color(255,0,0),250);
        while (abs(i) > BRAKETHRESHOLD) {
          lsm.read();
          currentX = abs(lsm.accelData.x);
          i = currentX - prevX;
          Serial.println(i);
          delay(100);
        }
        hideAll();
        brakeTime = millis();
        i = 0;
                  lsm.read();
          currentX = abs(lsm.accelData.x);
      }
    }
  }
}

  prevX = currentX;
  delay(200);
}

void check_switches()
{
  cState = digitalRead(cPin);
  dState = digitalRead(dPin);

  if (cState == HIGH) {     
    // left blinker
     Serial.println("left blink on"); 
    hideAll();
    leftTurn(Color(255,63,0),250);
    delay(300);
    Serial.println("left blink off");
    hideAll();
    delay(300);  
  }
  
  if (dState == HIGH) {     
    // right blinker
    Serial.println("right blink on"); 
    hideAll();
    rightTurn(Color(255,63,0),250);
    delay(300);
    Serial.println("right blink off");
    hideAll();
    delay(300);  
  }
}

void leftTurn(uint32_t c,uint8_t wait){
 innerLeftBottom(c);
 innerLeftTop(c);
   strip.show(); 
 delay(wait);
 hideAll();
 outerLeftTop(c);
 outerLeftBottom(c);
   strip.show(); 
 delay(wait);
 hideAll();
}

void rightTurn(uint32_t c,uint8_t wait){
 innerRightBottom(c);
 innerRightTop(c);
   strip.show(); 
 delay(wait);
 hideAll();
 outerRightTop(c);
 outerRightBottom(c);
   strip.show(); 
 delay(wait);
 hideAll();
}

void brakeLights(uint32_t c, uint8_t wait){
  innerRightBottom(c);
 innerRightTop(c);
 innerLeftBottom(c);
 innerLeftTop(c);
   strip.show(); 
 delay(wait);
 hideAll();
 outerLeftTop(c);
 outerLeftBottom(c);
 outerRightTop(c);
 outerRightBottom(c);
   strip.show(); 
 delay(wait);
 hideAll();
}


/* Helper functions */

//Input a value 0 to 384 to get a color value.
//The colours are a transition r - g - b - back to r

void outerRightBottom(uint32_t c){
  for (int i=0; i < 5; i++) {
    strip.setPixelColor(i, c);
  }
}
void outerRightTop(uint32_t c){
  for (int i=5; i < 10; i++) {
    strip.setPixelColor(i, c);
  }
}
void innerRightTop(uint32_t c){
  for (int i=10; i < 14; i++) {
    strip.setPixelColor(i, c);
  }
}
void innerRightBottom(uint32_t c){
  for (int i=14; i < 18; i++) {
    strip.setPixelColor(i, c);
  }
}

void innerLeftBottom(uint32_t c){
  for (int i=18; i < 22; i++) {
    strip.setPixelColor(i, c);
    strip.show();
  }
}

void innerLeftTop(uint32_t c){
  for (int i=22; i < 26; i++) {
    strip.setPixelColor(i, c);
  }
}

void outerLeftTop(uint32_t c){
  for (int i=26; i < 31; i++) {
    strip.setPixelColor(i, c);
  }
}
void outerLeftBottom(uint32_t c){
  for (int i=31; i < 36; i++) {
    strip.setPixelColor(i, c);
  }
}



void hideAll(){
  for(int i = 0; i < strip.numPixels();i++){
   strip.setPixelColor(i,Color(0,0,0));
  }
  strip.show();
}

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

Thanks!

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Flora Brakelight Backpack

Post by adafruit_support_bill »

Does the accelerometer work with the LSM303 test example?

alan1408
 
Posts: 7
Joined: Tue Feb 18, 2014 1:33 pm

Re: Flora Brakelight Backpack

Post by alan1408 »

Yes. It works perfectly. Does it have anything to do with the combined code?

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Flora Brakelight Backpack

Post by adafruit_support_bill »

Unfortunately, the accelerometer sensor is not working properly.
Can you be more specific? Are the reading incorrect, or is it not reading at all?

alan1408
 
Posts: 7
Joined: Tue Feb 18, 2014 1:33 pm

Re: Flora Brakelight Backpack

Post by alan1408 »

Now the accelerometer is working. But it will light up whenever I ran into a bumpy road. Another thing is the ultrasonic sensor and the accelerometer sensor cannot working simultaneously. I believe this has something to do with the code. Any ideas? Thanks!

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Flora Brakelight Backpack

Post by adafruit_support_bill »

Now the accelerometer is working. But it will light up whenever I ran into a bumpy road.
The code simply looks at the x-axis of the accelerometer. From that it can't tell the difference between forces generated by braking or those generated by bumps. You would need to look at all three axis and calculate the direction of the force vector. In the braking case, the force vector will be pointing mostly forward (x-axis). In the bumpy road case, it will be mostly vertical (z-axis).
Another thing is the ultrasonic sensor and the accelerometer sensor cannot working simultaneously.
I don't see anything there that would prevent them from working together. Although you do have some unnecessary delays in there.

alan1408
 
Posts: 7
Joined: Tue Feb 18, 2014 1:33 pm

Re: Flora Brakelight Backpack

Post by alan1408 »

In order to avoid the brakelight to light up if I ran into bumps, do I need to add lines in the code or just simply adjusting the Brakethreshod and Braketime value? Sorry that I'm asking too much questions.

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Flora Brakelight Backpack

Post by adafruit_support_bill »

Adjusting the threshold will make it less sensitive in general. But if you increase it too much you will miss gentile breaking.
Calculating the actual force vector requires some trigonometry. But a simple approach is to look at the Z-axis (vertical) forces. If the absolute value of the Z axis force is greater than the X-axis (front-to-back) force, then it is more likely due to a bump than to breaking action.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”