Go Down

Topic: Trouble making my LED's on Arduino blink (Read 286 times) previous topic - next topic

lucyjung

Jul 12, 2018, 05:23 am Last Edit: Jul 13, 2018, 04:59 am by lucyjung
Hello
My Led's are connected to adafruit L3GD20H gyroscope and i have successfully turned on the LED's which are working. However i want my lights to flash when it is on and have trouble making it flash! does anyone know how to make it blink? i need the indicator part which is pin 9 and 10 to flash!!

Help needed asap!!!!!
i have attached my whole file below :)
thanks!


Code: [Select]

#include <Wire.h>
#include <Adafruit_L3GD20_U.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>


bool brakeON = false;


/* Assign a unique ID to this sensor at the same time */
Adafruit_L3GD20_Unified gyro = Adafruit_L3GD20_Unified(20);

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void displaySensorDetails(void)
{
  sensor_t sensor;
  gyro.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" rad/s");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" rad/s");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" rad/s");
  Serial.println("------------------------------------");
  Serial.println("");


  accel.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" m/s^2");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" m/s^2");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" m/s^2");
  Serial.println("------------------------------------");
  Serial.println("");

  delay(100);
}

void displayDataRate(void)
{
  Serial.print  ("Data Rate:    ");

  switch (accel.getDataRate())
  {
    case ADXL345_DATARATE_3200_HZ:
      Serial.print  ("3200 ");
      break;
    case ADXL345_DATARATE_1600_HZ:
      Serial.print  ("1600 ");
      break;
    case ADXL345_DATARATE_800_HZ:
      Serial.print  ("800 ");
      break;
    case ADXL345_DATARATE_400_HZ:
      Serial.print  ("400 ");
      break;
    case ADXL345_DATARATE_200_HZ:
      Serial.print  ("200 ");
      break;
    case ADXL345_DATARATE_100_HZ:
      Serial.print  ("100 ");
      break;
    case ADXL345_DATARATE_50_HZ:
      Serial.print  ("50 ");
      break;
    case ADXL345_DATARATE_25_HZ:
      Serial.print  ("25 ");
      break;
    case ADXL345_DATARATE_12_5_HZ:
      Serial.print  ("12.5 ");
      break;
    case ADXL345_DATARATE_6_25HZ:
      Serial.print  ("6.25 ");
      break;
    case ADXL345_DATARATE_3_13_HZ:
      Serial.print  ("3.13 ");
      break;
    case ADXL345_DATARATE_1_56_HZ:
      Serial.print  ("1.56 ");
      break;
    case ADXL345_DATARATE_0_78_HZ:
      Serial.print  ("0.78 ");
      break;
    case ADXL345_DATARATE_0_39_HZ:
      Serial.print  ("0.39 ");
      break;
    case ADXL345_DATARATE_0_20_HZ:
      Serial.print  ("0.20 ");
      break;
    case ADXL345_DATARATE_0_10_HZ:
      Serial.print  ("0.10 ");
      break;
    default:
      Serial.print  ("???? ");
      break;
  }
  Serial.println(" Hz");
}

void displayRange(void)
{
  Serial.print  ("Range:         +/- ");

  switch (accel.getRange())
  {
    case ADXL345_RANGE_16_G:
      Serial.print  ("16 ");
      break;
    case ADXL345_RANGE_8_G:
      Serial.print  ("8 ");
      break;
    case ADXL345_RANGE_4_G:
      Serial.print  ("4 ");
      break;
    case ADXL345_RANGE_2_G:
      Serial.print  ("2 ");
      break;
    default:
      Serial.print  ("?? ");
      break;
  }
  Serial.println(" g");
}

void setup(void)
{
  Serial.begin(9600);
  Serial.println("Gyroscope Test"); Serial.println("");

  /* Enable auto-ranging */
  gyro.enableAutoRange(true);

  /* Initialise the sensor */
  if (!gyro.begin())
  {
    /* There was a problem detecting the L3GD20 ... check your connections */
    Serial.println("Ooops, no L3GD20 detected ... Check your wiring!");
    while (1);
  }

  /* Display some basic information on this sensor */
  displaySensorDetails();

  initializeLEDsSimple();

  Serial.println("Accelerometer Test"); Serial.println("");

  /* Initialise the sensor */
  if (!accel.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
    while (1);
  }

  /* Set the range to whatever is appropriate for your project */
  accel.setRange(ADXL345_RANGE_16_G);
  // displaySetRange(ADXL345_RANGE_8_G);
  // displaySetRange(ADXL345_RANGE_4_G);
  // displaySetRange(ADXL345_RANGE_2_G);

  /* Display some basic information on this sensor */
  displaySensorDetails();

  /* Display additional settings (outside the scope of sensor_t) */
  displayDataRate();
  displayRange();
  Serial.println("");


}

void loop(void)
{
  /* Get a new sensor event */
  sensors_event_t event;
  gyro.getEvent(&event);

  /* Display the results (speed is measured in rad/s) */
  Serial.print("X: "); Serial.print(event.gyro.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.gyro.y); Serial.print("  ");
  Serial.print("Z: ")  ; Serial.print(event.gyro.z); Serial.print("  ");
  Serial.println("deg/s ");

  indicatorLight(event.gyro.y, event.gyro.z, event.gyro.x);

  


  /* Get a new sensor event */
  accel.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.acceleration.z + 9.1); Serial.print("  "); Serial.println("m/s^2 ");
  Serial.println(" ");

  brakeLight(event.acceleration.x);


  delay(100);
}

void initializeLEDsSimple()
{
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

}



void brakeLight(float x)
{
  if (x <= -9 and x >= -11) {
    x = 0;
  }

  if (x <= -0.5) {
    brakeON = true ;
  }

  if (x > 0.5) {
    brakeON = false;
  }

  if (brakeON)
  {
    digitalWrite(11, HIGH);
    delay (50);
    Serial.println("Brake ON");
  }
  else
  {
    digitalWrite(11, LOW);
    delay (50);
    Serial.println("Brake OFF");
  }
}



  void indicatorLight(float y, float z, float x)

  {
    if (y > 1.2 or z < -0.5)

    {
      digitalWrite(9, HIGH);
      delay(500);
      digitalWrite(10, LOW);
      Serial.println("indicator ON");
      Serial.println("Rotating");

    }


    else if (y < -1.2 or z > 0.5)

    {
      digitalWrite(9, LOW);
      digitalWrite(10, HIGH);
      delay(500);
      Serial.println("indicator ON");
      Serial.println("Rotating");
    }
    else
    {
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
    }
 }
  






PaulRB

What kind of trouble are you having making the LEDs flash? I can't see anything in your sketch to make them flash. What have you tried?

Why do you need help so urgently? Its this a college assignment and you are late completing it?

Delta_G

|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Coding Badly


@lucyjung, please do not cross-post.  Other thread removed.


ieee488

Look at the example sketch    File ---> Examples ---> Digital ---> BlinkWithoutDelay


.

PaulRB

#5
Aug 05, 2018, 03:41 pm Last Edit: Aug 05, 2018, 03:42 pm by PaulRB
The OP is cross-posting again!

https://forum.arduino.cc/index.php?topic=557696.0

Reported to moderators.

Go Up