Plz Help; SWITCH PROBLEM (in NXC program idea) ??

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
onewinged
Posts: 11
Joined: 02 Dec 2011, 15:50

Plz Help; SWITCH PROBLEM (in NXC program idea) ??

Post by onewinged »

This program takes input from 2 touch sensors; where you have to Click the First touch sensor (1 or 2 times) then the Second touch sensor (1 or 2 times)
  • If the first touch sensor pressed 1 time -->it will move 1 meter
    If the first touch sensor pressed 2 times --> it will move 2 meters
    If the Second touch sensor pressed 1 time --> it will move 5 meters
    If the Second touch sensor pressed 2 times --> it will move 10 meters

Code: Select all

#include "NXCDefs.h"
bool button_bumped(const byte btn)
{  bool result = false;
   if (ButtonPressed(btn, false))
   { while (ButtonPressed(btn, true));
     PlayFile("! Click.rso");
      result = true;
   }
   return result;
}
//========================================================
task main() {
int touchCounter1, touchCounter2; // count how many times touch sensor pressed
SetSensor(IN_1, SENSOR_TOUCH);
SetSensor(IN_2, SENSOR_TOUCH);
TextOut (10, LCD_LINE1, "THE FIRST DISTANCE " );
touchCounter1 = 0;
while(!(button_bumped(BTNCENTER)) & touchCounter1<6 )
{
if(SENSOR_1 == 1) {
//TextOut (10, LCD_LINE4, " Pressed");
NumOut(10,LCD_LINE2,touchCounter1);
touchCounter1 = touchCounter1 + 1;
Wait(1000);
}
else {
TextOut (10, LCD_LINE2, " " );
//Wait(1000);
}
} // end while
NumOut(10,LCD_LINE2,touchCounter1);
TextOut (10, LCD_LINE3, "THE SCOND DISTANCE " );
touchCounter2 = 0;
while(!(button_bumped(BTNCENTER)))
{
if(SENSOR_2 == 1) {
//TextOut (10, LCD_LINE4, " Pressed");
NumOut(10,LCD_LINE4,touchCounter2);

touchCounter2 = touchCounter2 + 1;
Wait(1000);
}
else {
TextOut (10, LCD_LINE4, " " );
//Wait(1000);
}
} // end while
 NumOut(10,LCD_LINE4,touchCounter2);
switch(touchCounter1)  {
 case 1:
 {
switch(touchCounter2){
case 1:
{ 
do{
OnFwd(OUT_BC, 75);
while (MotorRotationCount(OUT_BC)<XX ); //I didn't calculate it yet X)
Off(OUT_BC);
} 
Case 2:
{ 
do{
OnFwd(OUT_BC, 75);
while (MotorRotationCount(OUT_BC)<XX ); //I didn't calculate it yet X)
Off(OUT_BC);
} 
}
Case 2:
//same before
}
}
} // end main

But it seems like It doesn't work
Plz help ..
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Plz Help; SWITCH PROBLEM (in NXC program idea) ??

Post by afanofosc »

Your code is complicated enough that it makes it hard to say exactly what it going wrong. And I can't test it myself without a lot of work and I don't know exactly what you have in mind for the "correct" behavior. I think it would help you and us if you were to write down in plain old English exactly what you want your program to do. Also tell us what you mean by "it seems like it doesn't work". What does it do?

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Plz Help; SWITCH PROBLEM (in NXC program idea) ??

Post by HaWe »

what if you try it to "say" it by
if...
else if....
else if...
else if...
?
(just the way you wrote it down in your pseudo-language schedule?
just remember:
KISS -
keep it simple and stupid.)
onewinged
Posts: 11
Joined: 02 Dec 2011, 15:50

Re: Plz Help; SWITCH PROBLEM (in NXC program idea) ??

Post by onewinged »

afanofosc wrote:Your code is complicated enough that it makes it hard to say exactly what it going wrong. And I can't test it myself without a lot of work and I don't know exactly what you have in mind for the "correct" behavior. I think it would help you and us if you were to write down in plain old English exactly what you want your program to do. Also tell us what you mean by "it seems like it doesn't work". What does it do?

John Hansen
ok I will try ^^ plz bear with me :) coz I need this Exact methodology for a bigger project ^-^

--begin--
define two int conter1 & counter2 < (the sum of counters (number of how many the touch sensor was pressed)

Claculate Number of times the first touch sensor is pressed --(assign to)-->touchCounter1
Calculate Number of times the Second touch sensor is pressed --(assign to)--> touchCounter2

-------------> till this step the program worked ...but it didn't go to switch I don't know why :(

Switch (touchCounter1):
case 1 //( first touch sensor pressed once => 1 m)
{
Switch (touchCounter2):
{
case 1 //(second touch sensor pressed once => 5 m)
Robot move 6 m forward // 1 m + 5 m ;
case 2 // (second touch sensor pressed twice => 10 m)
Robot move 11 m forward // 1 m + 10 m ;
} //end switch

case 2 // ( Second touch sensor pressed twice => 2 m)
{
Switch (touchCounter2):
{
case 1 //(second touch sensor pressed once => 5 m)
Robot move 7 m forward // 2 m + 5 m ;
case 2 // (second touch sensor pressed twice => 10 m)
Robot move 12 m forward // 2 m + 10 m ;
} //end switch


I think if else if would work , but I need to learn how to do it with switch ^_^
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: Plz Help; SWITCH PROBLEM (in NXC program idea) ??

Post by spillerrec »

What you are trying to do can be done much simpler:

Code: Select all

int meters = counter1 + counter2 * 5;
That is it. Here is a list to verify it:
  • None pressed: meters = 0 + 0*5 = 0
  • Touch 1 pressed once: meters = 1 + 0 * 5 = 1
  • Touch 1 pressed twice: meters = 2 + 0 * 5 = 2
  • Touch 1 not pressed, Touch 2 pressed once: meters = 0 + 1*5 = 5
  • Touch 1 pressed once, Touch 2 pressed once: meters = 1 + 1*5 = 6
  • Touch 1 pressed twice, Touch 2 pressed once: meters = 2 + 1*5 = 7
  • Touch 1 not pressed, Touch 2 pressed twice: meters = 0 + 2*5 = 10
  • Touch 1 pressed once, Touch 2 pressed twice: meters = 1 + 2*5 = 11
  • Touch 1 pressed twice, Touch 2 pressed twice: meter = 2 + 2*5 = 12
If you really want to do it with a switch it looks like this:

Code: Select all

int meters = -1;

switch( counter1 ){
	case 0:
			switch( counter2 ){
				case 0: meters = 0; break;
				case 1: meters = 5; break;
				case 2: meters = 10; break;
			}
		break;
	case 1:
			switch( counter2 ){
				case 0: meters = 1; break;
				case 1: meters = 6; break;
				case 2: meters = 11; break;
			}
		break;
	case 2:
			switch( counter2 ){
				case 0: meters = 2; break;
				case 1: meters = 7; break;
				case 2: meters = 12; break;
			}
		break;
}
Notice what happens if either Touch 1 or Touch 2 happens to be pressed more than twice. None of the cases are activated, so 'meters' didn't change and is still '-1'.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Plz Help; SWITCH PROBLEM (in NXC program idea) ??

Post by afanofosc »

So I didn't look closely enough but in C the keywords are case sensitive - "case" vs "Case" and each case falls through to the next case unless you use "break;" so that could be part of your problem. Look up the correct C syntax for "switch" in the NXC help or anywhere online using Google.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest