Programming Rules

Learning Intention

To understand some basic rules about programming

Reinforcing understanding of functions

Success Criteria

I can apply programming rules to fix errors

I can create and call a function

Review

Marked your GPA task. Good but need work on the following.

Functions:

  • Your can’t call a function if it doesn’t exit (not defined).
    360(1000) ?
  • If you define (create) a function you have to name it
    _____(2000) { } x
  • If your passing a value to a function the function has to have an argument
    forward(1000)   | forward() { } x

General Arduino Code:

  • All instructions need to end with a semicolon ;
    forward(1000)  x  | forward(1000);  correct
  • We use an open brace { to open a function, loop or if statement
  • We use a close brace } to close a function, loop or if statement
  • Spelling of functions and variables has to be the same including upper and lower case.
    forward(1000);  | Forward(int pause) { } x  |  forward(1000);  | forward(int pause) { } correct

Elaborate

Take your GPA tasks from Monday and fix up the mistake on a new sheet.

Explore

Build a robot to the next stage, your teacher will tell you the next 2 or 3 steps.

Test

Use the programs below on a robot to see it works. Then create and test your dancing robot code.

//Motor A 
int motorAspeed = 10; // MCU PWM Pin 10 to ENA  
int motorAforward = 9;  // MCU Digital Pin 9 to IN1  
int motorAback = 8;  // MCU Digital Pin 8 to IN2  

//Motor B 
int motorBspeed = 5;  // MCU PWM Pin 5 to ENB  
int motorBforward = 7;  // MCU Digital pin 7 to IN3  
int motorBback = 6;  // MCU Digital pin 6 to IN4  

void setup() { 
  pinMode(motorAspeed, OUTPUT); 
  pinMode(motorBspeed, OUTPUT); 
  
  pinMode(motorAforward, OUTPUT); 
  pinMode(motorAback, OUTPUT); 
  
  pinMode(motorBforward, OUTPUT); 
  pinMode(motorBback, OUTPUT);   
}

void loop()
{ 
 forward(); 
 delay(1000);
 reverse();
 delay(1000);  
}  

void forward() {
  analogWrite(motorAspeed, 200);
  digitalWrite(motorAforward, HIGH); // Turn HIGH motor A
  digitalWrite(motorAback, LOW);
  
  analogWrite(motorBspeed, 200);  
  digitalWrite(motorBforward, HIGH); // turn HIGH motor B
  digitalWrite(motorBback, LOW);  
}

void reverse() {
  analogWrite(motorAspeed, 200);
  digitalWrite(motorAforward, LOW); // Turn HIGH motor A
  digitalWrite(motorAback, HIGH);
  
  analogWrite(motorBspeed, 200);
  digitalWrite(motorBforward, LOW); // turn HIGH motor B
  digitalWrite(motorBback, HIGH);  
}

3

Is your robot not driving forwards and backwards in a straight line? Is it not driving at all? Click on the image below to see the wiring diagram, and double check your robot’s wiring

H-Bridge Thumb

Review

Did you get your robot working correctly today? If not what happened and how did you try and fix it?

Print Friendly, PDF & Email