Using If statements to control a case switch

I want to have a sonic sensor report a value, and if that value is smaller than a certain number, I want the code to switch to a certain case.

Code:

#include <PRIZM.h> // Includes the PRIZM Library


 PRIZM prizm; 


int batteryVoltage;

int leftDistance;

int centerDistance;

int rightDistance;

int frontDistance;

int lineSensor;

int driveDirection;

int difference = 0;



// int difference = leftDistance - rightDistance;




u8 sensorValue[4];

float distanceForward = 30.48;         // target distance forward (cm)

float wheelRadius = 4.091;             // radius of drive wheels (cm)

int encoderStepsPerRevolution = 3955;  // number of steps per revolution (3955 for ARC motors, 1440 for Tetrix Torqnado motors) ***Tune this number for your robot.***

int speed = 150;                


void setup() {
  
  prizm.PrizmBegin();

  prizm.setMotorInvert(1, 0);

  prizm.setMotorInvert(2, 1);


  Serial.begin(9600);



}

void loop() {
  // put your main code here, to run repeatedly:

  sensorReadings();

  if(frontDistance <= 18)

  switch(driveDirection) {
    case 1:
      if(leftDistance >= 5.5) {
        move_left();
      } else {
        move_right();
      }
      break;
  }

  
  
    
  if(rightDistance >= 4.5) {

    move_left();
    
  } else {

    move_right();

  }



  

  if(difference <= 0) {

    move_forwards();

  }

 

  

  if(centerDistance <= 18) {

    move_backwards();

  }

  prizm.resetEncoders();



  


}



void sensorReadings(){

  batteryVoltage = prizm.readBatteryVoltage();

  leftDistance = prizm.readSonicSensorIN(2);

  centerDistance = prizm.readSonicSensorIN(3);

  rightDistance = prizm.readSonicSensorIN(4);

  // lineSensor = prizm.readLineSensor(5);

  frontDistance = prizm.readSonicSensorIN(5);


}

void move_right(){
  prizm.setMotorPowers(20, 25);
  delay(90);


}

void move_forwards(){
  prizm.setMotorPowers(30, 30);
  delay(10);
  
}

void move_left(){
  prizm.setMotorPowers(25, 20);
  delay(90);


}

Hello. @roboticsisfun - Does your code work? Do you have a question?

Welcome to the forum

It is not clear what you want to do.

You have a switch/case in your sketch but only one case. The switch uses the value of driveDirection but that value never changes

You could do something like this

sensorReading = readSensor();
if (sensorReading > 0 && sensorReading <=100)
{
  driveDirection = 1;
}
else if (sensorReading > 10 && sensorReading <=200)
{
  driveDirection = 2
}

switch case (driveDirection)
{
  case 1:
  //code here for direction 1
  break;
  case 2:
  //code here for direction 2
  break;
}

Is that the sort of thing you had in mind ?

1 Like

You have defined this variable as int

and.....
Have tested with float value.

1 Like

look this over

String test;

enum { ST_A, ST_B, ST_C };
int state = ST_A;

void stMach (
    char stim )
{
    switch (state)  {
    case ST_A:
        if ('c' == stim)  {
            state = ST_C;
            Serial.println (" ST_C");
        }
        break;

    case ST_B:
        if ('a' == stim)  {
            state = ST_A;
            Serial.println (" ST_A");
        }
        break;

    case ST_C:
        if ('b' == stim)  {
            state = ST_B;
            Serial.println (" ST_B");
        }
        break;
    }
}

void loop ()
{

    if(Serial.available() > 0){
        char c = Serial.read ();
        stMach (c);
    }
}


void setup() {
    Serial.begin(115200);
}

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