Notifications
Clear all

New Member

25 Posts
5 Users
16 Likes
1,497 Views
(@rallard)
Member
Joined: 4 years ago
Posts: 14
Topic starter  

Hi My name is Robert,

This is my first time in the forum .

I was a technician, and now I am trying to be able to write sketches.

So far I am having trouble with my 1st sketch!

It has to do with running the L298n motor controller basic sketch.

It compiles until it gets to the Decelerates from max. to zero

It does the for ( int I = 255; I >= 0; --i);

[

But gets hung up on the   analogWrite(enA, i);

The error is ( I was not declared in this scope ))How coold I fix that?


   
Inst-Tech reacted
Quote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

@rallard

Try changing the I to i in the for loop.

for ( int i= 255; i >= 0; --i);

C++ is case sensitive and you apparently have not defined i anywhere.
Defining a variable means this,
int i; // define i as an integer
which I am guessing is the case when you set up the for loop with I when you meant i.

 


   
Biny reacted
ReplyQuote
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
 

Welcome! I hope you enjoy your time here. By way of a tip, when pasting code for feedback / debugging help, a single line is not really sufficient context. There is also a "code" format block (the <> symbol above the text you're writing) that helps with readability - so assuming your code looks something like this:

for( int I=255;  I>=0; --i );
   analogWrite( enA, i);

 There are two issues - the first of which is that you have a mixed case, with the first two parts of the for loop conditions using capital I, and the pre-decrement operator (--i) using lower case. The int statement in the first bit of the for loop declares the I to be in scope for the loop, but not the lower case i.

The second issue is that the for loop ends with a semi-colon in your code, which means that the loop does nothing in it's body - it's a null loop. Stylistically, when writing a null loop, I would format it as such:

for( int i=255; i>=0; --i )
   ;

I recommend a couple different things - first, always declare your variables into existence on a separate line, and always indent your loop body, even if it's a null loop - and even better, always use braces for loop bodies, as it is explicitly clear in that case what is looped.

const int enA = 9; // Analog pin for motor
int i; // Loop index variable

for( i=255; i >=0; --i) {
   analogWrite( enA, i);
}

 

 

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
ron bentley and sj_h1 reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@rallard

Welcome to the forum 🙂

Besides the obvious error in upper/lower case mentioned, it seems extremely strange that you should be trying to set the enable pin to any value other than 0 (zero) or 1 (one).

This suggests that you have a logic or comprehension problem as well, so feel free to post your code and ask for help if you need more advice.

Anything seems possible when you don't know what you're talking about.


   
Inst-Tech reacted
ReplyQuote
(@rallard)
Member
Joined: 4 years ago
Posts: 14
Topic starter  

@will Thank You Will.

I don't think version of windows allows me to run sketches!

I will go over my sketch again to find out what else is wrong.


   
ReplyQuote
(@rallard)
Member
Joined: 4 years ago
Posts: 14
Topic starter  

@binaryrhyme Thank You very much for your advice


   
Biny reacted
ReplyQuote
(@rallard)
Member
Joined: 4 years ago
Posts: 14
Topic starter  

@robotbuilder Thank you for your help.


   
ReplyQuote
(@rallard)
Member
Joined: 4 years ago
Posts: 14
Topic starter  

#include <Servo.h>

//L298N Motor Control Demonstration with 2 Potentiometers
// L298N-Motor-Control- Demo-2pots.ino
// Demonstrates use of 2 potentiometer with Arduino and L298N Motor Controller

DroneBot Workshop 2017
http://dronebotworkshop.com
*/

// Motor A

int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B
int enB = 3;
int in3 = 5;
int in4 = 4;

//Speed control potentiometers

int SpeedControl1 = A0;
int SpeedControl2 = A1;

//Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

void setup()

{
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);

}

void loop() {
// Set Motor A Forward

digitalwrite(in1, HIGH);
digitalwrite(in2, LOW);

// Set Motor B Forward

digitalwrite(in3, HIGH);
digitalwrite(in4, LOW);

// Read the values from the potentiometers

MotorSpeed1 = analogRead(SpeedControl1);
MotorSpeed2 = analogRead(speed Control2);

// Convert to renge of 0-255

MotorSpeed1 = map(MotorSpeed, 0,1023, 0, 255;
MotorSpeed2 = map(MotorSpeed, 0,1023, 0, 255;

// Adjust to prevent 'buzzing' at very low speed

if (MotorSpeed1 = 0)MotorSpeed1 = 0;
if (MotorSpeed2 = 0)MotorSpeed2 = 0;

// Set the Motor speeds

analogwrite(enA, Motorspeed1);
analogwrite(enB, Motorspeed2);
}


   
ReplyQuote
(@rallard)
Member
Joined: 4 years ago
Posts: 14
Topic starter  

@robotbuilder 


   
ReplyQuote
(@rallard)
Member
Joined: 4 years ago
Posts: 14
Topic starter  

@robotbuilder

/*
L296N Motor Demonistration
L298N Motor-Demo.inoo
Demonisrtates functions of L298N Motor Controller

DroneBot Workshop 2017
http://dronebotworkshop.com <
*/

// Motor A

int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B

int enB = 3;
int in3 = 5;
int in4 = 4;

void setup()

{

// Set all the motor control pins to output

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

}

void demo0ne()

{

// This function will run the motors in both directions at a fixed speed

// turn on motor A

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Set speed to 200 out of possible range 0-255

analogWrite(enA, 200);

// Turn on motor B

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

// Set speed to 200 out of possible range 0-255

analogWrite(enB, 200);

delay(2000);

// Now change motor directions

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

delay(2000);

// Now turn off motors

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}
void demoTwo()

{

// This function will run the motors acrossthe range of possible speeds
// Note that maximum speed is determined by the motor itself and the operating voltage

// Turn on motors

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

// Accelerate from zero to maximum speed

for (int i = 0; i = 256; i++)

[

analogWrite(enA, i);
analogWrite(enB, i);

delay(20);

]

// Decelerate motors from maximum speed to zero
for(int i = 255; i >= 0; i--);

[

analogWrite(enA; i);
analogWrite(enB; i);

delay(20);

}

// Now turn off motors

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}

void loop()

{

demo0ne()

delay(1000);

demoTwo()

delay(1000);

}

Sorry, binaryrhyme; This is the sketch that would not compile


   
ron bentley reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7024
 

@rallard Looks like you are missing the includes, let me check.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7024
 

@rallard There are a number of simple errors that the compiler is pointing out. I fixed those and now it compiles. It almost looks like you have a keyboard issue, or a char-set issue as a few things like {} were displayed as [] and caused an error. If you are unable to fix the errors that the compiler points out let us know, I have a working copy.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7024
 

@rallard Did you select the UNO board?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7024
 

@rallard Here is the corrected code

// Motor A

int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B

int enB = 3;
int in3 = 5;
int in4 = 4;

void setup()

{

// Set all the motor control pins to output

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

}

void demo0ne()

{

// This function will run the motors in both directions at a fixed speed

// turn on motor A

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Set speed to 200 out of possible range 0-255

analogWrite(enA, 200);

// Turn on motor B

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

// Set speed to 200 out of possible range 0-255

analogWrite(enB, 200);

delay(2000);

// Now change motor directions

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

delay(2000);

// Now turn off motors

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}
void demoTwo()

{

// This function will run the motors acrossthe range of possible speeds
// Note that maximum speed is determined by the motor itself and the operating voltage

// Turn on motors

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

// Accelerate from zero to maximum speed

for (int i = 0; i == 256; i++) /* Equality check is == assignment is = */

{ /* Wrong bracket, sb { */

analogWrite(enA, i);
analogWrite(enB, i);

delay(20);

} /* Wrong bracket, sb } */

// Decelerate motors from maximum speed to zero
for(int i = 255; i >= 0; i--) /* The ; ends the loop and also the scope of i causing error re undeclared in line 103 */

{ /* Wrong bracket, sb { */

analogWrite(enA, i); /* , between args, not ; */
analogWrite(enB, i); /* , between args, not ; */

delay(20);

}

// Now turn off motors

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}

void loop()

{

demo0ne(); /* Missing ; */

delay(1000);

demoTwo(); /* Missing ; */

delay(1000);

}

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ron bentley reacted
ReplyQuote
(@rallard)
Member
Joined: 4 years ago
Posts: 14
Topic starter  

@zander Thank You Very Much Ron.

I am very new at writing sketches as you can see.

You have been very helpful.

I do not know how to fix errors at all thank you


   
ReplyQuote
Page 1 / 2