forcing into while loop once first conditions are met - not working!

Hi all,

I have a custom board with the Atmega2560 and on board sensors for an embedded project at work (we manufacture machines).

I am having difficulty with getting it to work correctly and maybe I need a fresh set of eyes to look at the syntax.

basically there is a resistance sensor waiting for the resistance to be in-between 25ohm and 40,000ohm.
in the code there is a function outside the loop for this.

once the conditions are met a safety sensor should then take over waiting for the voltage to drop below 20v. I want the code to stay in this part until the condition is met and then return to beginning again, i have a while statement for this.

the while statement is waiting for an int to change from 0 to 1 before it loops through which is declared once the resistance conditions are met. i have tried a few different ways of laying out the topology of these two parts but i either get the resistance part working fine but the voltage part not doing anything or nothing depending on how the code has been written.

the best version of the code is below, please some one take a fresh look over this. its greatly appreciated.

int Ao = 0; //reference to the resistance sensor pin.
int raw = 0; // setting raw to be 0.
int Vin = 5; // setting Vin to be 5 (reference to 5 volts entering).
int SA = A15; // input of sensor monitoring for Safety Activation.
int Reset = 6; // Hardwired pin on 75CU-0001 that controls the Reset Relay.
int Safety = 11; // pinout to external Safety relay. 
int SwitchPosition = 0; // variable to tell the main loop what position to be in.
float Vout = 0; // Variable for resistance calculation.
float VSA;  // Variable for safety voltage sensor.
float R1 = (1000); // Variable for resistance calculation.
float R2 = 0; // Variable for resistance calculation.
float buffer = 0; // setting chip buffer to 0 before each loop.
const unsigned int MAX_INPUT = 300; // 300bytes allocated on chip for Data flow.

//-------------------------------------------------------------------------------------------------------------------

void setup()
{
  Serial.begin(115200);
  pinMode(Reset, OUTPUT);
  pinMode(Safety, OUTPUT);
  digitalWrite(Reset, HIGH); // means the reset relay will be switched off on power on.
  digitalWrite(Safety, LOW); // means the safety relay will be switched off on power on.
}

//-------------------------------------------------------------------------------------------------------------------

//Below are the functions to be called from the main loop


int Reset_Sensor() // resistence sensor on board.
{
  raw = analogRead(Ao);
  if (raw) {
    buffer = raw * Vin;
    Vout = (buffer) / 1024.0;
    buffer = (Vin / Vout) - 1;
    R2 = (R1 * buffer);
    {
      if (((R2) > 25) && ((R2) < 40000)) // resistance window to be measured for reset.
      {
        return 1;
      }
      else
      {
        return 0;
      }
    }
  }
}



int Safety_Activation()  // safety sensor (voltage) on board.
{
  float tempSA;
  tempSA = analogRead(SA) / 4.092;
  VSA = (tempSA / 10);
  if (((VSA) < 20))
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

void loop()    
{
  digitalWrite(Reset, HIGH);  // to ensure reset relay is off at the start of each loop.
  digitalWrite(Safety, LOW);  // to ensure safety relay is off at the start of each loop.
  
  if((Reset_Sensor()) ==1)    
    {
    digitalWrite(Reset, LOW);  // turns reset relay on.
    SwitchPosition = 1;
    } 
  while((SwitchPosition) == 1)
      if((Safety_Activation()) ==1)
      {
      digitalWrite(Safety, HIGH); // turns safety relay on.
      SwitchPosition = 0;
      }
      break;
}

I see a couple of questionable things:

  1. In your Reset_Sensor() function if you were to actually read a 0 value into raw then you do not return anything from the function.
  2. Why is there a break statement after this while loop?
  while((SwitchPosition) == 1)
      if((Safety_Activation()) ==1)
      {
      digitalWrite(Safety, HIGH); // turns safety relay on.
      SwitchPosition = 0;
      }
      break;

for an embedded project at work

So you come to a hobby forum for free engineering help with your commercial product?

I sometimes offer free help for commercial projects, if the OP states it openly. What I'm not so sure about, is this "safety" switch. If it has anything to do with protecting humans or property, I'm staying well away. Involvement in that sort of thing can lead to lawsuits, in addition to possible injury or death. I know because I've been involved in controllers for stamping machines.