Digital Write Output not working

I am making a fall detecting device for a project in school and in order for it to work, I need to be able to set pin12 to high when a fall is detected (line 83). However even when a fall is detected and displayed in the serial monitor, pin 12 remains set to low. It is not a hardware problem, as with other code it seems to work fine, however when I attempt to implement the digital write into the fall detecting code it does not seem to work.

Here is the code I am currently using:

#include <SPI.h>
#include "LCD_Driver.h"
#include "GUI_Paint.h"
#include "image.h"
#include "Wire.h"
const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
float ax=0, ay=0, az=0, gx=0, gy=0, gz=0;

//int data[STORE_SIZE][5]; //array for saving past data
//byte currentIndex=0; //stores current data array index (0-255)
boolean fall = false; //stores if a fall has occurred
boolean trigger1=false; //stores if first trigger (lower threshold) has occurred
boolean trigger2=false; //stores if second trigger (upper threshold) has occurred
boolean trigger3=false; //stores if third trigger (orientation change) has occurred

byte trigger1count=0; //stores the counts past since trigger 1 was set true
byte trigger2count=0; //stores the counts past since trigger 2 was set true
byte trigger3count=0; //stores the counts past since trigger 3 was set true
int angleChange=0;

void setup(){
  pinMode(12, OUTPUT);
  Config_Init();
  LCD_Init();
  LCD_SetBacklight(1000);
  Paint_NewImage(LCD_WIDTH, LCD_HEIGHT, 0, LIGHTBLUE);
  Paint_Clear(WHITE);
  Paint_DrawLine  (120, 0, 120, 12,BLACK ,DOT_PIXEL_4X4,LINE_STYLE_SOLID);
  Paint_DrawLine  (120, 228, 120, 240,BLACK ,DOT_PIXEL_4X4,LINE_STYLE_SOLID);
  Paint_DrawLine  (0, 120, 12, 120,BLACK ,DOT_PIXEL_4X4,LINE_STYLE_SOLID);
  Paint_DrawLine  (228, 120, 240, 120,BLACK ,DOT_PIXEL_4X4,LINE_STYLE_SOLID);
  Paint_DrawLine  (120, 120, 70, 70,BLACK ,DOT_PIXEL_3X3,LINE_STYLE_SOLID);
  Paint_DrawLine  (120, 120, 176, 64,BLACK ,DOT_PIXEL_3X3,LINE_STYLE_SOLID);
  Paint_DrawLine  (120, 120, 120, 210,RED ,DOT_PIXEL_2X2,LINE_STYLE_SOLID); 
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  mpu_read();
  //2050, 77, 1947 are values for calibration of accelerometer
  // values may be different for you
  ax = (AcX-2050)/16384.00;
  ay = (AcY-77)/16384.00;
  az = (AcZ-1947)/16384.00;

  //270, 351, 136 for gyroscope
  gx = (GyX+270)/131.07;
  gy = (GyY-351)/131.07;
  gz = (GyZ+136)/131.07;

  // calculating Amplitute vector for 3 axis
  float Raw_AM = pow(pow(ax,2)+pow(ay,2)+pow(az,2),0.5);
  int AM = Raw_AM * 10;  // as values are within 0 to 1, I multiplied 
                         // it by for using if else conditions 

  Serial.println(AM);
  //Serial.println(PM);
  //delay(500);

  if (trigger3==true){
     trigger3count++;
     //Serial.println(trigger3count);
     if (trigger3count>=10){ 
        angleChange = pow(pow(gx,2)+pow(gy,2)+pow(gz,2),0.5);
        //delay(10);
        Serial.println(angleChange); 
        if ((angleChange>=0) && (angleChange<=10)){ //if orientation changes remains between 0-10 degrees
            fall=true; trigger3=false; trigger3count=0;
            Serial.println(angleChange);
              }
        else{ //user regained normal orientation
           trigger3=false; trigger3count=0;
           Serial.println("TRIGGER 3 DEACTIVATED");
        }
      }
   }
  if (fall==true){ //in event of a fall detection
    Serial.println("FALL DETECTED");
      digitalWrite(12, HIGH);
       delay(10000);
       digitalWrite(12, LOW);

    fall=false;
   // exit(1);
    }
  if (trigger2count>=6){ //allow 0.5s for orientation change
    trigger2=false; trigger2count=0;
    Serial.println("TRIGGER 2 DECACTIVATED");
    }
  if (trigger1count>=6){ //allow 0.5s for AM to break upper threshold
    trigger1=false; trigger1count=0;
    Serial.println("TRIGGER 1 DECACTIVATED");
    }
  if (trigger2==true){
    trigger2count++;
    //angleChange=acos(((double)x*(double)bx+(double)y*(double)by+(double)z*(double)bz)/(double)AM/(double)BM);
    angleChange = pow(pow(gx,2)+pow(gy,2)+pow(gz,2),0.5); Serial.println(angleChange);
    if (angleChange>=30 && angleChange<=400){ //if orientation changes by between 80-100 degrees
      trigger3=true; trigger2=false; trigger2count=0;
      Serial.println(angleChange);
      Serial.println("TRIGGER 3 ACTIVATED");
        }
    }
  if (trigger1==true){
    trigger1count++;
    if (AM>=12){ //if AM breaks upper threshold (3g)
      trigger2=true;
      Serial.println("TRIGGER 2 ACTIVATED");
      trigger1=false; trigger1count=0;
      }
    }
  if (AM<=2 && trigger2==false){ //if AM breaks lower threshold (0.4g)
    trigger1=true;
    Serial.println("TRIGGER 1   ACTIVATED");
    }
//It appears that delay is needed in order not to clog the port
  delay(100);
}

void mpu_read(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
}



/*********************************************************************************************************
  END FILE
*********************************************************************************************************/


Thank you so much

Spi uses pin 12 on an Uno, so you probably need to pick some other pin for your output.

1 Like

is this the same even if I am using just the atmega 328P-PU

also what pins would you suggest that I use, as shown in the image below, a large amount of the. pins are already occupied.

If you are using a custom design microprocessor board, and you now have an issue and are looking at the code…

...you should go back to the prototype that worked on an Arduino board, where you developed all the code and circuitry and tested it and stuff and…

...run through some of that process again. Move back to the custom circuits when all is well.

a7

1 Like

Hi,
Can I suggest you change the IDE monitor baud rate;
From;

Serial.begin(9600);

to;

Serial.begin(115200);

Does the serial monitor actually display;

 Serial.println("FALL DETECTED");

Check to see if you can use one of the analog inputs, just pinMode it as an OUTPUT, to make it a digital OUTPUT.

Thanks.. Tom.. :+1: :coffee: :australia:

Tell us where you got this idea so we can avoid getting our ideas from that place.

And no part of any if or if/else statement is a loop.

a7

Yes.

what pins would you suggest that I use, as shown in the image below, a large amount of the. pins are already occupied.

How about PD6 (Arduino pin 6, I think.) And you have a bunch of portC pins free as well - they can be used for digital IO if they're not needed for AnalogInput.

I hope you have AVCC and that 2nd ground pin connected to power. They're NOT optional!

I ended up changing the pin to A3 and it seemed to work, as in the pin was set to high. However my esp8266-01 which is connected to ground and vcc to A3, is turning on, however it isn’t doing its intended task which is to send a http request to WhatsApp bot and in turn send me a notification. The esp works when it is plugged into the usb to esp programming board, however it does not work when the vcc and gnd are connected only.

To clarify, when the esp 8266 is plugged I into the esp programmer, and is either turned on or reset, a message is sent, however when only the gnd and vcc pins are connected to a power source (I’ve tried 5v, 3.3v, 3v and 6v), the module turns on, but the http request isn’t sent.

Yes the serial monitor does display “fall detected”

Are you using an I/O pin to supply power to the 8266?

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Here is the full circuit diagram

My bad i forgot to label it but H1 is a MPU6050

Hi,

Reset button should be connected to GND.

Check your C1 and C2 values.

image
I assume you have the 8L version. (3v3)

How much current is drawn by the 8266?
Have you measured the 3v3 input to the 8266 when it is active?

Tom.... :smiley: :+1: :coffee: :australia:

im not sure how to do this, as I don't have an ammeter

Hi,

Either way, I am currently prototyping with an Arduino nano, I intend on only utilising the atmega in the final PCB once I am able to get my prototype working with the nano.

Thanks :slight_smile:

Hi, @claush1

Do you have a DMM? (Digital MultiMeter)

If not, you will be better off with one when you are prototyping from the ground up.

Tom.. :smiley: :+1: :coffee: :australia:

I think I have one, Ill have to look and see.

Oh dear. Better test it with some simple code. That kind of voltage can blow it instantly.