#include <Servo.h> // Include the servo library
Servo myservo; // Create a new Servo Object
#define MIN 0 // Minimum servo angle
#define MAX 180 // Maximum servo angle
int angle = 90;
int input= 0;
void setup() {
Serial.begin(9600);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
void loop() {
if (Serial.available()){
input = Serial.read();
}
Serial.println(input);
switch (input) {
case 'F':
digitalWrite(7, HIGH); //Sets direction "Forward“, (pin 7 High & pin 8 Low)
digitalWrite(8,LOW);
break;
case 'B':
Serial.println("Backward");
digitalWrite(7,LOW); //Set direction "Backward“, (pin 7 Low & pin 8 High)
digitalWrite(8, HIGH);
break;
case 0:
analogWrite(6, 0); //Controls a PWM signal on pin 6
break;
case 1:
analogWrite(6, 28); //Controls a PWM signal on pin 6
break;
case 2:
analogWrite(6, 56); //Controls a PWM signal on pin 6
break;
case 3:
analogWrite(6, 84); //Controls a PWM signal on pin 6
break;
case 4:
analogWrite(6, 112); //Controls a PWM signal on pin 6
break;
case 5:
analogWrite(6, 128); //Controls a PWM signal on pin 6
break;
case 6:
analogWrite(6, 168); //Controls a PWM signal on pin 6
break;
case 7:
analogWrite(6, 196); //Controls a PWM signal on pin 6
break;
case 8:
analogWrite(6, 224); //Controls a PWM signal on pin 6
break;
case 9:
analogWrite(6, 255); //Controls a PWM signal on pin 6
break;
case 'L':
myservo.write(angle); // Set servo to current angle // wait a half-second
angle -= 10; // increase angle variable by 10 degrees
case 'R' :
myservo.write(angle); // Set servo to current angle // wait a half-second
angle += 10; // increase angle variable by 10 degrees
}
}
I was programing a switch case for a robot. I wasn't getting an error but there were no out puts for either the servo or the motor.
Set your compiler warning level to ALL, and leave it there. It'll save much tearing of hair now and in the future.
arduino-cli compile -b arduino:avr:uno --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Uno_R3/test)
/home/me/Documents/sketchbook/Uno_R3/test/test.ino: In function 'void loop()':
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:79:16: warning: this statement may fall through [-Wimplicit-fallthrough=]
angle -= 10; // increase angle variable by 10 degrees
~~~~~~^~~~~
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:81:7: note: here
case 'R':
^~~~
Sketch uses 6212 bytes (19%) of program storage space. Maximum is 32256 bytes.
Global variables use 241 bytes (11%) of dynamic memory, leaving 1807 bytes for local variables. Maximum is 2048 bytes.
Compilation finished successfully.
This will always result in the rather useless number "10" being printed because the character you send to the serial buffer is followed by a "13" for carriage-return and a "10" for linefeed. Maybe you will get one "twitch" of a RLFB, but then it does not know what to do with the "10"... perhaps discard the value. Have a read on serial basics... Serial Input Basics - updated
Idea is to react only on new key, if there is some.
The code say otherwise.
If Serial is nor available, it gets no read, BUT it will print the old value and react on it.
As human is slow to 16MHz processor, there will be too much of this happening, so Serial output line may became full and start blocking the output. (especially, if the last input was B printing Backwards)
Also it would be impossible to steer a little. Pressing R would add 10 to angle and before you can press anything else, it would be repeated like hundred times, adding like thousends to angle.
Also as you do not use break in the case of L, it will fall thru from it down, executing all the commands to next break.
Pressing L would decrease angle by ten, then increase angle by ten.
The cases with numbers will not be normally reachable, as pressing key with 1/! on it will sends character ‘1’ which value is 31, not 1.
(You may be able to reach the case 9 by pressing the TAB key. )
This comment adds virtually nothing: the operative words and values are directly in the code (unless you need a reminder you're working with pins). Conversely
(aside from the missing breaks) there's mention of a "wait". But there's no delay(500) here for either. If one is added, almost no reason to put a comment on it, unless you want a reminder that the value is in milliseconds.
Both comments say "increase", but the -= is decreasing. Less important, but "angle variable by 10" again adds nothing; "degrees", just a smidge.
On a different and more on-topic note: a case can cover a range of values. So some code can be reduced, like this
The ... indicates a range. The char values '0' through '9' are contiguous.
(As are 'A' through 'Z' and 'a' through 'z'
unless you are using EBCDIC instead of ASCII/Unicode
No worries, you're not)
The values assigned don't match an obvious linear calculation, so the somewhat arbitrary values can be hard-coded in an array
Because a new variable is introduced for this case, the whole thing has to be in a block, surrounded by the { } curly braces
Use [] without a count so that the compiler will count how many values you typed; then use static_assert to make sure you didn't forget any or have too many
Because the char-keys are contiguous, subtracting '0' computes the effective index -- zero for the first element
However useful the comment "Controls a PWM signal on pin 6" is: if you add it, at least you don't have to repeat it ten times.
I'll work on implementing these things into the code and will post the updated code with more questions most likely. Thank you for all your help all of you!! I really appreciate it!!!