View unanswered posts | View active topics It is currently Thu Oct 02, 2014 2:09 pm






Reply to topic  [ 6 posts ] 
ubyte in a "switch" statement 
Author Message
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post ubyte in a "switch" statement
I have the following code:

Code:
#pragma config(Sensor, S1, NoBttn, sensorTouch)
#pragma config(Sensor, S2, YesBttn, sensorTouch)
#pragma config(Sensor, S3, LightSens, sensorLightInactive)
#pragma config(Sensor, S4, HTIRL, sensorI2CCustom)
#pragma config(Motor, motorC, TumblrMtr, tmotorNormal, PIDControl, reversed, encoder)

#include "drivers/HTIRL-driver.h";

void initialize();
void InitializePF();

const ubyte IsConnected[1] = {1};
const ubyte IAmConnected[1] = {2};
const ubyte DoneSorting[1] = {3};
const ubyte StartSorting[1] = {4};
const ubyte Okay[1] = {5};
const ubyte StopConveyors[1] = {6};


ubyte SentMessage[1];

tPFmotor Tread1AndLights = pfmotor_S4_C1_A;
tPFmotor Tread2 = pfmotor_S4_C1_B;
tPFmotor DispenserMtr = pfmotor_S4_C2_A;
tPFmotor PneumSwtch = pfmotor_S4_C2_B;
tPFmotor BrickScannerDisp = pfmotor_S4_C3_A;
tPFmotor ColorScanAlign = pfmotor_S4_C3_B;

ePWMMotorCommand Tread1AndLightsFwd = MOTOR_FWD_PWM_7;
ePWMMotorCommand Tread2Fwd = MOTOR_FWD_PWM_7;
ePWMMotorCommand DispenserMtrDisp = MOTOR_FWD_PWM_7;
ePWMMotorCommand PneumClawUp = MOTOR_FWD_PWM_7;
ePWMMotorCommand PneumClawDwn = MOTOR_REV_PWM_7;
ePWMMotorCommand BrickScannerDispExt = MOTOR_FWD_PWM_3;
ePWMMotorCommand BrickScannerDispRtr = MOTOR_REV_PWM_3;
ePWMMotorCommand ColorScanAlignExt = MOTOR_REV_PWM_3;
ePWMMotorCommand ColorScanAlignRtr = MOTOR_FWD_PWM_3;

task main()
{
  initialize();
 
  while(SentMessage[0] != DoneSorting[0])
  {
    cCmdMessageRead(SentMessage[0], 1, 0);
   
    switch(SentMessage[0])
    {
      case StartSorting[0]:
        PFMotor(Tread1AndLights, Tread1AndLightsFwd);
        PFMotor(Tread2, Tread2Fwd);
      break;
      case StopConveyors[0]:
        PFMotor(Tread1AndLights, MOTOR_BRAKE);
        PFMotor(Tread2, MOTOR_BRAKE);
      break;
    }
  }
}

void initialize()
{
  while(SentMessage[0] != IsConnected[0]){cCmdMessageRead(SentMessage[0], 1, 0);}
  cCmdMessageWriteToBluetooth(IAmConnected[0], 1, 0);
 
  InitializePF();
}
void InitializePF()
{
  PFMotor(ColorScanAlign, ColorScanAlignRtr);
  wait1Msec(500);
  PFMotor(ColorScanAlign, MOTOR_BRAKE);
 
  PFMotor(BrickScannerDisp, BrickScannerDispRtr);
  wait1Msec(500);
  PFMotor(BrickScannerDisp, MOTOR_BRAKE);
}


On lines 50 and 54 I get the error: **Error**:'case' statement expression must have constant value

What does this mean? Am I doing something wrong?

_________________
A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.


Tue Feb 21, 2012 5:58 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: ubyte in a "switch" statement
NeXT-Generation wrote:
On lines 50 and 54 I get the error: **Error**:'case' statement expression must have constant value

What does this mean? Am I doing something wrong?

Your case statement is using StartSorting[0] which is a variable. You can only use a constant or an expression that can be evaluated to a constant at compile time in case statements. Since StartSorting[0] is initialized to 4 at compile time, why can't you just do "case 4" instead? If you want to give it a meaningful name, you can do the following:
Code:
#define START_SORTING          4

switch (SendMessage[0])
{
    case START_SORTING:
        break;
    ...
}


Tue Feb 21, 2012 6:44 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: ubyte in a "switch" statement
I guess that MHTS answered your question, but I'd like to offer a tip. Next time, if you want to do something like this, and the compiler won't let you, you can declare your variable a constant, i.e. const int c = 5; Sometimes, even though it's probably not good programming practice, this will allow you to shove a value into the function anyway if you know that it's correct.

_________________
sudo rm -rf /


Tue Feb 21, 2012 7:30 pm
Profile
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post Re: ubyte in a "switch" statement
MHTS wrote:
NeXT-Generation wrote:
On lines 50 and 54 I get the error: **Error**:'case' statement expression must have constant value

What does this mean? Am I doing something wrong?

Your case statement is using StartSorting[0] which is a variable. You can only use a constant or an expression that can be evaluated to a constant at compile time in case statements. Since StartSorting[0] is initialized to 4 at compile time, why can't you just do "case 4" instead? If you want to give it a meaningful name, you can do the following:
Code:
#define START_SORTING          4

switch (SendMessage[0])
{
    case START_SORTING:
        break;
    ...
}


I suppose the #define will work for me. I have two NXTs passing lots of messages back and forth, and I wanted actual names instead of numbers. Just one thing: the variables WERE constants, so why the error?

_________________
A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.


Tue Feb 21, 2012 8:12 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: ubyte in a "switch" statement
I could be wrong but I don't think any variable would work, even with const.


Tue Feb 21, 2012 8:23 pm
Profile
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post Re: ubyte in a "switch" statement
MHTS wrote:
I could be wrong but I don't think any variable would work, even with const.

Apparently not. Anyway, using #define works perfectly. :programmer: Big thanks for the help!! :bigthumb:

_________________
A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.


Wed Feb 22, 2012 12:56 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.