|
Page 1 of 1
|
[ 6 posts ] |
|
ubyte in a "switch" statement
| Author |
Message |
|
NeXT-Generation
Senior Roboticist
Joined: Wed Sep 28, 2011 10:13 pm Posts: 510 Location: Totally not spying on Hassenplug to see what he has for the Brickworld Chicago 2013 sumo contest.
|
 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 |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 Re: ubyte in a "switch" statement
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:
|
| Tue Feb 21, 2012 6:44 pm |
|
 |
|
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 495
|
 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 |
|
 |
|
NeXT-Generation
Senior Roboticist
Joined: Wed Sep 28, 2011 10:13 pm Posts: 510 Location: Totally not spying on Hassenplug to see what he has for the Brickworld Chicago 2013 sumo contest.
|
 Re: ubyte in a "switch" statement
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 |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 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 |
|
 |
|
NeXT-Generation
Senior Roboticist
Joined: Wed Sep 28, 2011 10:13 pm Posts: 510 Location: Totally not spying on Hassenplug to see what he has for the Brickworld Chicago 2013 sumo contest.
|
 Re: ubyte in a "switch" statement
Apparently not. Anyway, using #define works perfectly.  Big thanks for the help!! 
_________________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 |
|
|
|
Page 1 of 1
|
[ 6 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 4 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
|
|