Sorry for my bad English, my native language is Spanish
When you enclosed in a CASE block. a bad tabulation is done
void convertTochar_16seg(int c) {
switch (c) {
case 0: {
char_0_ON();
...
break;
}
case 1: {
char_1_ON();
...
break;
}
}
}
must be
void convertTochar_16seg(int c) {
switch (c) {
case 0: {
char_0_ON();
...
break;
}
case 1: {
char_1_ON();
...
break;
}
}
}
pert
August 9, 2016, 12:19am
2
The problem is those braces are not required. If you Auto Format the examples on the reference page(https://www.arduino.cc/en/Reference/SwitchCase ):
switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
or:
switch (var) {
case 1:
{
//do something when var equals 1
int a = 0;
}
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
The reason you're seeing what appears to be an extra indentation is because the case label causes one indentation of the lines until the next case label, including the braces. Your braces cause another indentation of the lines they enclose. Putting the opening brace on the same line as the case label is valid syntax but confusing. It's the equivalent of this:
if (c == 0) {{
char_0_ON();
}
}
Also valid code but the indentation looks wrong. In fact with this code Auto Format will move the second open brace to the next line:
if (c == 0) {
{
char_0_ON();
}
}
Hello
in the body or block of CASE no {} are required, but have benefits
1: Be more readable, the instruction block
is similar to
if (digitalRead(MY0_ENDSTOP_MIN_INTPIN) == LOW) {
run_Y(DWY, 1, convRoundsToStep(20));
}else{
Serial.println("******************* ERROR: Sensor Y0 activo");
}
run_X(DWY, 1, convRoundsToStep(10));
...
if (digitalRead(MY0_ENDSTOP_MIN_INTPIN) == LOW)
run_Y(DWY, 1, convRoundsToStep(20));
else
Serial.println("******************* ERROR: Sensor Y0 activo");
run_X(DWY, 1, convRoundsToStep(10));
...
2: allow collapse and expand, code block, especially when the code is very long, see attached image
I hope that the Arduino TEAM correct this indented in the next release
Who agrees with the indented improved?
+1
#1 , Disagree. I find the { } very helpful for keep track of code that is to run with a for, else, while, switch:case, etc.
#2 . That could be useful. I use the IDE tab feature to achieve similar hiding/expanding.
without tabulation and unplaced {} is a cause of error in programming time, especially for beginners
if (digitalRead(MY0_ENDSTOP_MIN_INTPIN) == LOW)
run_Y(DWY, 1, convRoundsToStep(20));
else
Serial.println("******************* ERROR: Sensor Y0 activo");
run_X(DWY, 1, convRoundsToStep(10));
...
scope of variables if correctly tab
int f1() {
{
int v1;
{
int v2;
{
int v3;
}
}
}
...
}
pert
August 10, 2016, 9:12pm
6
compsystems:
in the body or block of CASE no {} are required, but have benefits
I realize that. I frequently use them to allow variables to be declared within the case. As I said before, this is not a bug, the Auto Format is specifically configured to do exactly what it's doing which in this case I think is absolutely correct. The problem is the screwy way you insist on formatting your code. However, the great thing about the Artistic Style formatter is it's very configurable. To get the indentation behavior you want you only have to:
Open the file {Arduino IDE installation folder}/lib/formatter.conf in a text editor
Remove the line:
indent-cases
Save the file
Restart the Arduino IDE.
Problem solved!
I think it's trivial improve indented, if the CASE block contains containers {}, fit the tab, Who is able to change this in the source code?
I wish that every time my code run on different computers, the tabulation is as follows,
int setupDriver8825( int motorN, int microstep_div) { // entrada: micropasos o divicion de un paso que puede efectuar el accionador del motor
int microstep_mode0;
int microstep_mode1;
int microstep_mode2;
// Configuracion del DRIVER segun el motor
if (motorN == MOTOR_X0) {
microstep_mode0 = DRIVER8825_MX0_MODE0_PIN;
microstep_mode1 = DRIVER8825_MX0_MODE1_PIN;
microstep_mode2 = DRIVER8825_MX0_MODE2_PIN;
}
else { //if (motorN == MOTOR_Z0)
microstep_mode0 = DRIVER8825_MZ0_MODE0_PIN;
microstep_mode1 = DRIVER8825_MZ0_MODE1_PIN;
microstep_mode2 = DRIVER8825_MZ0_MODE2_PIN;
}
driverMicrostep = microstep_div;
switch (driverMicrostep) {
case 1: { // fullStep: Mayor torque, movimientos bruscos y mayor ruido
digitalWrite(microstep_mode0, LOW);
digitalWrite(microstep_mode1, LOW);
digitalWrite(microstep_mode2, LOW); // 000 = 0
motorStepsPerRev = motorStepsPerRev_factory * microstep_div; // 200*1=200 pasos para dar una vuelta del motor
anglePerStep = rev / motorStepsPerRev; // 360/200 = 9/5 = 1.8º/paso
// 200 pasos * 1.8 deg = 360 deg == 1 vuelta
break;
}
case 2: { // halfStep: Menor torque, movimientos suaves y menor ruido
digitalWrite(microstep_mode0, HIGH);
digitalWrite(microstep_mode1, LOW);
digitalWrite(microstep_mode2, LOW); // 100 =
motorStepsPerRev = motorStepsPerRev_factory * microstep_div; // 200*2=400
anglePerStep = rev / motorStepsPerRev; // 360/400 = 9/10 = 0.9º/paso
// 400 pasos * 0.9 deg = 360 deg
break;
}
case 4: { // microStep1_4: Menor torque aun, movimientos mucho mas suaves y mucho menos ruido
digitalWrite(microstep_mode0, LOW);
digitalWrite(microstep_mode1, HIGH);
digitalWrite(microstep_mode2, LOW); // 010 = 2
motorStepsPerRev = motorStepsPerRev_factory * microstep_div; // 200*4=800
anglePerStep = rev / motorStepsPerRev; // 360/800 = 9/20 = 0.45º/paso
// 800 pasos * 0.45 deg = 360 deg
break;
}
case 8: { // microStep1_8: ...
digitalWrite(microstep_mode0, HIGH);
digitalWrite(microstep_mode1, HIGH);
digitalWrite(microstep_mode2, LOW); // 110 = 2
motorStepsPerRev = motorStepsPerRev_factory * microstep_div; // 200*8=1600
anglePerStep = rev / motorStepsPerRev; // 360/1600 = 9/40 = 0.225º/paso
// 1600 pasos * 0.225 deg = 360 deg
break;
}
case 16: { // microStep1_16: ...
digitalWrite(microstep_mode0, LOW);
digitalWrite(microstep_mode1, LOW);
digitalWrite(microstep_mode2, HIGH); // 001 = 1
motorStepsPerRev = motorStepsPerRev_factory * microstep_div; // 200*16=3200
anglePerStep = 360.0 / motorStepsPerRev; // 360/3200 = 9/80 = 0.1125º/paso
// 3200 pasos * 0.1125 deg = 360 deg
break;
}
case 32: { // microStep1_32: ...
driverMicrostep = 32;
digitalWrite(microstep_mode0, HIGH);//HIGH/LOW
digitalWrite(microstep_mode1, HIGH);//LOW/HIGH
digitalWrite(microstep_mode2, HIGH);
motorStepsPerRev = motorStepsPerRev_factory * microstep_div; // 200*32=6400
anglePerStep = rev / motorStepsPerRev; // 360/6400 = 9/160 = 0.05625º/paso
// 6400 pasos * 0.05625 deg = 360 deg
break;
}
}
}