LED and accelerometer code

Hello! I am a beginner trying to make my code work the way I want it to. I am building a circuit on Tinkercad which is an online website for building circuits. What I am trying to do with my circuit is make the LEDs light up when the x-axis value on the accelerometer is 0 and the LEDs should not flash but should stay on. As of now this code here works but I do not know where to make the adjustments or what else to add so that I get it to work on how I want.

The code:

//set ovulation temperature range
const int ovulation_a = 90;
const int ovulation_b = 120;


//set variables
const int x_pin = A0; 
const int y_pin = A1; 
const int z_pin = A3; 


const float sensitivity = 0.8; //given in datasheet 
const float Vref = 5.0; //voltage reference 
const float Vzerog = 1.8; //voltage at zero G (as given in datasheet)  


float x;
float y;  
float z;


//circuit setup
void setup() 
{ 
  pinMode(A2, INPUT); //sensor
  pinMode(2, OUTPUT); //blue LED
  pinMode(3, OUTPUT); //green LED
  pinMode(4, OUTPUT); //red LED
  
  analogReference(EXTERNAL); 
  Serial.begin(9600); 
} 


//circuit behaviour
void loop() 
{ 
  
  //motion sensor
  {
  x = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);  
  Serial.print("x: ");
  Serial.println(x); 


  delay(1000); //take a reading every second (ms)
  }


  //temperature sensor
  {
  int sensor = analogRead(A2);
  float voltage = (sensor / 1024.0) * 5.0;
  float tempC = (voltage - 0.5) * 100;
  float tempF = (tempC * 1.8) + 32;
  Serial.print("temp: ");
  Serial.print(tempF);
  
  delay(1000);
    
  //only show results if resting
  if (tempF >= ovulation_a && tempF <= ovulation_b && x > 0) 
  { //ovulating
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    Serial.println(" Ovulating");  
  }
  
  else 
  { //not ovulating
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    Serial.println(" Not Ovulating");
  }
  }
}

surrounding blocks of code with braces is unnecessary. i think you believe it's doing something it's not

within each block you have a 1000 msec delay. so loop repeat every 2 sec. only 1 delay is necessary.

what are the values of x, and tempF when you think the code is not working properly?

The tempF range the LEDs should light up is 90-120. This is fine. As for the accelerometer when x=0 it should indicate that the person is not moving hence if the temperature is within the range 90-120 the LEDs should light up which they are not. I have to move the accelerometer and then the LEDs will light up. Once the accelerometer goes back to x=0 the LEDs do not light up anymore.

The new code after removing 1 delay:

//set ovulation temperature range
const int ovulation_a = 90;
const int ovulation_b = 120;


//set variables
const int x_pin = A0; 
const int y_pin = A1; 
const int z_pin = A3; 


const float sensitivity = 0.8; //given in datasheet 
const float Vref = 5.0; //voltage reference 
const float Vzerog = 1.8; //voltage at zero G (as given in datasheet)  


float x;
float y;  
float z;


//circuit setup
void setup() 
{ 
  pinMode(A2, INPUT); //sensor
  pinMode(2, OUTPUT); //blue LED
  pinMode(3, OUTPUT); //green LED
  pinMode(4, OUTPUT); //red LED
  
  analogReference(EXTERNAL); 
  Serial.begin(9600); 
} 


//circuit behaviour
void loop() 
{ 
  
  //motion sensor
  {
  x = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);  
  Serial.print("x: ");
  Serial.println(x); 


  }


  //temperature sensor
  {
  int sensor = analogRead(A2);
  float voltage = (sensor / 1024.0) * 5.0;
  float tempC = (voltage - 0.5) * 100;
  float tempF = (tempC * 1.8) + 32;
  Serial.print("temp: ");
  Serial.print(tempF);
  
  delay(1000);
    
  //only show results if resting
  if (tempF >= ovulation_a && tempF <= ovulation_b && x > 0) 
  { //ovulating
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    Serial.println(" Ovulating");  
    //wait for 100 miliseconds
  }
  
  else 
  { //not ovulating
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    Serial.println(" Not Ovulating");
  }
  }
}

not sure in which case, ovulating or not ovulating, the LEDs light up.

since x presumably represents some acceleration in a particular direction, the ovulating case will only be true while accelerating in one direction (i.e. x > 0)

since x is a float, i doubt its value is every actually 0.0000. it would be better to test that its absolute value is less than some small value (ABS(x) < 0.1) (don't remember if it ABS() or abs())

Thank you! That works now! I have another question. How can I create an array of all the temperatures recorded when the body is in resting state. That would be as we said abs(x)<0.1
I want this so that I can be able to store and calculate the average body temperature when the person is at rest.

The new code:

//set ovulation temperature range
const int ovulation_a = 90;
const int ovulation_b = 120;


//set variables
const int x_pin = A0; 
const int y_pin = A1; 
const int z_pin = A3; 


const float sensitivity = 0.8; //given in datasheet 
const float Vref = 5.0; //voltage reference 
const float Vzerog = 1.8; //voltage at zero G (as given in datasheet)  


float x;
float y;  
float z;


//circuit setup
void setup() 
{ 
  pinMode(A2, INPUT); //sensor
  pinMode(2, OUTPUT); //blue LED
  pinMode(3, OUTPUT); //green LED
  pinMode(4, OUTPUT); //red LED
  
  analogReference(EXTERNAL); 
  Serial.begin(9600); 
} 


//circuit behaviour
void loop() 
{ 
  
  //motion sensor
  {
  x = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);  
  Serial.print("x: ");
  Serial.println(x); 


  }


  //temperature sensor
  {
  int sensor = analogRead(A2);
  float voltage = (sensor / 1024.0) * 5.0;
  float tempC = (voltage - 0.5) * 100;
  float tempF = (tempC * 1.8) + 32;
  Serial.print("temp: ");
  Serial.print(tempF);
  
  delay(1000);
    
  //only show results if resting
  if (tempF >= ovulation_a && tempF <= ovulation_b && abs(x) < 0.1) 
  { //ovulating
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    Serial.println(" Ovulating");  
    //wait for 100 miliseconds
  }
  
  else 
  { //not ovulating
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    Serial.println(" Not Ovulating");
  }
  }
}

decide on a array size, define the array and an index, capture values incrementing the index up to the array size

untested

   constant int MaxTemps  = 100;
    float tempFs [MaxTemps];
    int    idx = 0;




    if (MaxTemps > idx)
        tempFs [idx++] = tempF;

I tried rearranging and putting the code where I think it should be. However I am not sure if it is keeping track of different temperatures in the array and I do not know ow to check. I guess the next step after getting the array to work would be calculating the average temperature and would need a code like:

if(MaxTemps<100) {
tempF(MaxTemps)= adcRead();
else{
avgtempFs=sumArray();
}
}
//set ovulati
on temperature range
const int ovulation_a = 90;
const int ovulation_b = 120;
  
//set array range for temperature
const int MaxTemps  = 100;


//set variables
const int x_pin = A0; 
const int y_pin = A1; 
const int z_pin = A3; 


const float sensitivity = 0.8; //given in datasheet 
const float Vref = 5.0; //voltage reference 
const float Vzerog = 1.8; //voltage at zero G (as given in datasheet)  


float x;
float y;  
float z;




//circuit setup
void setup() 
{ 
  pinMode(A2, INPUT); //sensor
  
  pinMode(12, INPUT); //red LED
  
  pinMode(2, OUTPUT); //blue LED
  pinMode(3, OUTPUT); //green LED
  pinMode(4, OUTPUT); //red LED
  
  analogReference(EXTERNAL); 
  Serial.begin(9600); 
  
  
} 


//circuit behaviour
void loop() 
{ 
  if(digitalRead(12)==HIGH)
  {
  //motion sensor
  {
  x = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);  
  Serial.print("x: ");
  Serial.println(x); 


  }


  //temperature sensor
  {
  int sensor = analogRead(A2);
  float voltage = (sensor / 1024.0) * 5.0;
  float tempC = (voltage - 0.5) * 100;
  float tempF = (tempC * 1.8) + 32;
  Serial.print("temp: ");
  Serial.print(tempF);
  
     ///Recording Reference Temperature
    float tempFs [MaxTemps];
    int    idx = 0;




    if (MaxTemps > idx)
        tempFs [idx++] = tempF;
    
  delay(1000);
    
  //only show results if resting
  if (tempF >= ovulation_a && tempF <= ovulation_b && abs(x) < 0.1) 
  { //ovulating
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    Serial.println(" Ovulating");  
    //wait for 100 miliseconds
  }
  
  else 
  { //not ovulating
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    Serial.println(" Not Ovulating");
  }
  }
  }
  else {
   	//Get Reference Temperature
  }
}

You've got a problem with your index variable "idx" - it gets reinitialised each time through loop().

Either make it global, or make it static.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.