Help to construct multiple command at the same time

Hello everybody,

I am working on a project for past couple of weeks, but I wasn’t able to successfully make it work as the requirement. Please need help ASAP as much as you can.

I am working on a project to build a lifting cart. I am using arduino UNO as a micro controller. That controls two commands

  1. when battery charger is plugged in and the battery level is LOW turn on the LED alarm and turn On the switch to charge the battery. I have two sensors
    a) to read voltage level
    b) to read if charger on and off

  2. lifting the platform of the cart up and down. and we have two position sensor.

So it works like this,

IF battery is good and charger is not plugged in , you can lift the platform up/down using a switch.

If battery is LOW and charger is connected, you can’t lift the platform. and turn on the charging switch. (basically dont send any command)

If battery is low and charger is not connected, you can’t lift the platform. and turn on Led alarm.

If battery is really low it will flash the LED.

Layout is attached below.
The layout has two sensors. 1) to measure voltage , 2) to check battery charging

So, this is the code to lifting the platform up/down. And it checks for the battery voltage. The only thing I am having problem is, I don’t know how to check if charger is connected and then cut off the motor to lift the platform.

int Switch = 8;                 // LED connected to digital pin 13
boolean sensorValue = A1;
boolean voltage ;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
float value = 0;
#define downSwitchPin 2            // Down signal from the switch        (INPUT)
#define upSwitchPin      3            // Up signal from the switch          (INPUT)
#define dirPin             4            // DIR signal pin to the motor driver (OUTPUT)
#define pwmPin             5            // PWM signal pin to the motor driver (OUTPUT)  
#define upSensorPin      6            // Up height sensor                   (INPUT)
#define downSensorPin 7            // Down height sensor                 (INPUT)




int   upSwitchFlag,
      downSwitchFlag,
      upSensorFlag,
      downSensorFlag,
      dirSignal,
      dutyCycleMax;

float pwmSignal,
      pwmIncrement;

enum
{ READY,
  CHARGE,
  STOP,                        // Rest state
  ACCEL_UP,              // Accelerating up state
  MOVE_UP,               // Moving up state
  DECEL_UP,              // Decelerating up state
  ACCEL_DOWN,       // Accelerating down state
  MOVE_DOWN,        // Moving down state
  DECEL_DOWN
}      // Decelerating down state
stateVar;
void setup()
{
  Serial.begin(9600);
  pinMode(Switch, OUTPUT);      // sets the digital pin as output
  pinMode(sensorValue, INPUT_PULLUP);      // sets the digital pin as output
  pinMode(downSwitchPin, INPUT);
  pinMode(upSwitchPin,      INPUT);
  pinMode(dirPin,                OUTPUT);  // Blue wire from harness
  pinMode(pwmPin,            OUTPUT);  // Green wire from harness
  pinMode(downSensorPin, INPUT);
  pinMode(upSensorPin,      INPUT);
  pinMode(13,                      OUTPUT);  // Strictly to keep LED 13 LOW & conserve battery

  upSwitchFlag      = 0;           // Flag that alerts Arduino that Up has been pressed
  downSwitchFlag = 0;           // Flag that alerts Arduino that Down has been pressed
  upSensorFlag      = 0;           // Flag that alerts Arduino that maximum height has been reached
  downSensorFlag = 0;           // Flag that alerts Arduino that minimum height has been reached
  pwmSignal          = 0;           // PWM signal strength to extend/retract the linear actuator
  dirSignal             = 0;           // Signal for dir pin of motor driver
  dutyCycleMax    = 255;       // (0-255)/255 * 100% (duty cycle)
  pwmIncrement    = 0.25;      // Amount to increment/decrement PWM signal
  stateVar               = READY;   // Initial state


}

void loop()
{

  // read the input on analog pin 0:
  sensorValue = analogRead(A1);    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  if (sensorValue == HIGH) {
    voltage = true ;
  }
  if  (sensorValue == LOW) {
    voltage = false ;
  }

  // read the value at analog input
  value = analogRead(A0);
  vout = (value * 5) / 1024.0; // see text
  vin = vout / (R2 / (R1 + R2));

  upSwitchFlag = digitalRead(upSwitchPin);
  downSwitchFlag  = digitalRead(downSwitchPin);

  switch (stateVar) {


    case READY:
      pwmSignal = 0;
      dirSignal = LOW;

      if (vin <= 12 && (voltage == true)) {
        //digitalWrite(Switch, LOW);   // sets the LED on
        stateVar  = CHARGE;
      }

      else if (vin >= 16 && (voltage == false)) {
        digitalWrite(Switch, LOW);    // sets the LED off
        stateVar  = STOP;

      }


    case CHARGE:
      if (vin <= 12 && (voltage == true)) {
        digitalWrite(Switch, HIGH);
        digitalWrite(Switch, HIGH);    // sets the LED off
        //stateVar  = STOP;
      }
      else if (vin >= 13 && (voltage == false)) {
        digitalWrite(Switch, LOW);    // sets the LED off
        stateVar  = STOP;
      }


    case STOP:                     // Default (rest) state with no outputs from PWM and DIR
      pwmSignal = 0;
      dirSignal = LOW;

      if (upSwitchFlag   == HIGH) stateVar = ACCEL_UP;
      if (downSwitchFlag == HIGH) stateVar = ACCEL_DOWN;
      break;

    case ACCEL_UP:      // Accelerating up state from minimum PWM signal & LOW DIR signal
      pwmSignal += pwmIncrement;
      dirSignal  = LOW;

      if (pwmSignal >= dutyCycleMax) {
        pwmSignal = dutyCycleMax;
        dirSignal = LOW;
        stateVar  = MOVE_UP;
      }

      if (upSwitchFlag == LOW) stateVar = DECEL_UP;
      break;

    case MOVE_UP:                  // Moving up state with maximum PWM & LOW DIR signal
      pwmSignal = dutyCycleMax;
      dirSignal = LOW;

      if (upSwitchFlag == LOW) stateVar = DECEL_UP;
      break;

    case DECEL_UP:          // Decelerating up state to minimum PWM signal & LOW DIR signal
      pwmSignal -= pwmIncrement;
      dirSignal  = LOW;

      if (pwmSignal <= 0) {
        pwmSignal = 0;
        dirSignal = LOW;
        stateVar  = STOP;
      }
      break;

    case ACCEL_DOWN: // Accelerating down state from min. PWM signal & HIGH DIR signal
      pwmSignal += pwmIncrement;
      dirSignal  = HIGH;

      if (pwmSignal >= dutyCycleMax) {
        pwmSignal = dutyCycleMax;
        dirSignal = HIGH;
        stateVar  = MOVE_DOWN;
      }

      if (downSwitchFlag == LOW) stateVar = DECEL_DOWN;
      break;

    case MOVE_DOWN:    // Moving down state with maximum PWM & HIGH DIR signal
      pwmSignal = dutyCycleMax;
      dirSignal = HIGH;

      if (downSwitchFlag == LOW) stateVar = DECEL_DOWN;
      break;

    case DECEL_DOWN:   // Decelerating down state to min. PWM signal & HIGH DIR signal
      pwmSignal -= pwmIncrement;
      dirSignal  = HIGH;


      if (pwmSignal <= 0) {
        pwmSignal = 0;
        dirSignal = LOW;
        stateVar  = STOP;
      }
      break;
  }


  analogWrite(pwmPin,  pwmSignal); // Write pwmSignal to pwmPin output
  digitalWrite(dirPin, dirSignal);          // Write dirSignal to dirPin output



}

boolean sensorValue = A1; boolean voltage ;

These variables aren't being used like they contain only true and false. They seem to be used as if they are integers. But that won't work very well, will it?

Also, don't use generic names like sensorValue. If this is the sensor that senses if the charger is connected or not, then use a name like sensorCharger or, as a boolean, chargerIsConnected.

For telling the system when to stop, I would create a boolean variable something like mustStopNow and that variable would be set according to each of the conditions you defined on the sensors. Then each state can decide how to respond to that. If you are currently running up, then mustStopNow will transition to the DECEL_UP state, so that it decelerates to a clean stop.

Hmmmm….. battery chargers and lift motors. I hope this isn't something where somebody can get hurt if your code F*cks Up.

loop() {
 if(the battery is really low) {
   flash the LED (using blink without delay method) 
 } 
 else if(the battery is low) {
    turn LED on
  } 
  else {
    turn LED off
  }

  if(battery is not ok and charger not plugged in) {
    turn alarm on
  }  
  else {
    turn alarm off
  }

  if(battery is ok) {
    if(switch is up and we have not hit the top stop) {
      move motor up
    }
    else if(switch is down and we have not hit the bottom stop) {
      move motor down
    }
    else {
      turn motor off
    }
  }
  else {
    turn motor off
  }
}