Arduino cookbook 8.13

I am new to Arduino! I have bought the Cookbook and in trying to run 8.13,m how to run a stepper motor using an EasyDriver Board. I have copied the code, word for word but I get the error message 'ch' was not declared in this scope. Help!
Phil Clement

Post the code you have. Presumably there is a typo somewhere.

const int dirPin = 2;
const int stepPin = 3;
const int enPin = 4;

int speed = 100;
int steps = 0;
long last_step = millis();
long timeout = 30*1000;

void setup()
{
// put your setup code here, to run once:
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(enPin,OUTPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
{
if (millis() > last_step + timeout)
{
digitalWrite(enPin,HIGH);
}

if (Serial.available())
{
char ch = Serial.read();

if(isDigit(ch)); //is ch a number?

steps = steps * 10 + ch - '0'; //yes accumulate the value
}
else if (ch == '+')
{
steps(steps);
steps = 0;
}
else if (ch == '-')
}
else if (ch =='s')
{
speed = steps;
Serial.print("Setting speedto "); Serial.println(steps);
steps = 0
}
}
}
void step(int steps)
}
int stepDelat = 100 / speed;
int stepsLeft;
digital/Write(enPin,LOW;
last_step = millis();
if (steps > 0)

digitalWrite(dirPin, HIGH);
stepsLeft = steps;

if (steps < 0)
digitalWrite(dirPin, LOW);
stepsLeft = -steps;
digitialWrite(stepPin,HIGH);
delayMicroseconds(1);
digitalWrite(stepDelay);
delay(stepDelay);
stepsLeft--;

You have declared ch here:

    if (Serial.available())
    {
      char ch = Serial.read();
      if (isDigit(ch)); //is ch a number?
      steps = steps * 10 + ch - '0'; //yes accumulate the value
    }

So it only exists in that scope, but you try to use it later on. I suggest that you take another look at the code in the book.

There's a syntax error there.

Hi Dougie, well spotted but not the solution, i have changed the code to know affect. Phil

Hi Wildbill, talk about a quick response! I am a real beginner, can you explain a bit more please; 'so it only exists in that scope' *and * 'try to use it later on'.
The code is word perfect from the book-I think.
Phil

No, it really isn't the same.

ch exists between the two braces in the fragment I posted. It ceases to exist at the closing brace.

By the time you get to this line:

else if (ch == '+')

ch no longer exists which is what the compiler is complaining about.

@philclement -can you give a link to where you download the book from please.

Hi,
Is this the code:

/*
 * Stepper_bipolar sketch
 *
 * stepper is controlled from the serial port.
 * a numeric value followed by '+' or '-' steps the motor
 *
 *
 * http://www.arduino.cc/en/Reference/Stepper
 */
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 24
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 2, 3, 4, 5);
int steps = 0;

void setup()
{
 // set the speed of the motor to 30 RPM
 stepper.setSpeed(30);
 Serial.begin(9600);
}
void loop()
{
 if ( Serial.available()) {
 char ch = Serial.read();
 if(isDigit(ch)){ // is ch a number?
 steps = steps * 10 + ch - '0'; // yes, accumulate the value
 }
 else if(ch == '+'){
 stepper.step(steps);
 steps = 0;
 }
 else if(ch == '-'){
 stepper.step(steps * -1);
 steps = 0;
 }
 }
}

It compiles for me, yours has some editing problems.
This might be part of your problem

void loop() {
// put your main code here, to run repeatedly:
{

two { one basically after the other.

Tom... :grinning: :+1: :coffee: :australia:

Hi Missdrew,

i have a hard copy of the book, photos attached.
Phil

You need to go back and read that code again, you HAVE got mistakes.

@philclement, your topic was moved to a more suitable location on the forum.

There are errata (corrections) and downloadable example code from the publisher:

1 Like

yes, right here: if (isDigit(ch)); //is ch a number?
his book has it right as does tom's

Is this the code?

Download all examples from the book, so you won't have to type them manually.
Extract to your sketch folder, then restart the IDE.

Hi,

Found this;
https://www.cplusplus.com/reference/cctype/isdigit/

The function compiled, I might have a play with it tonight, midday here, and see what it will tolerate.
The description says it takes an int.... I've never come across isDigit() before.

Tom... :grinning: :+1: :coffee: :australia:

I was referring to the semi-colon where there should be a opening brace.