Category Archives: Software

Teensy: PWM – Better Square Pattern

What is PWM? What is the benefit of using PWM pins? Which pins can be used for PWM output?
Look at this video below around 12:48 minutes into it and follow what he does.

Now try again the square pattern with more accurate performance. Copy and paste your program here.

// Square Pattern with "PWM" is pulse width modulation. 
// Using the PWM pins allows the programmer to control the pulses sent to the motors. 
// Pins 5 and 6 are used for PWM output. 
// Darcy Chang 
// 6/6/19 
// Arduino 1.8.5 

// Set up pins
const int OnBoardLED = 13; 
int Dlay = 500; 

void setup() 
{ 
  pinMode(9,OUTPUT); // set the pin modes to output 
  pinMode(10, OUTPUT); 
  pinMode(11,OUTPUT); 
  pinMode(12,OUTPUT); 
  pinMode(OnBoardLED,OUTPUT); 
  pinMode(5,OUTPUT); 
  pinMode(6,OUTPUT); 
} 

void loop() 
{ 
  blinky(); // blink the light 
  forward(1000,120,120); // move forward 
  stopBot(1000); // briefly pause 
  turnRight(1,80); // turn at a right angle 
} 

void blinky() 
{ 
  digitalWrite(OnBoardLED, LOW); // flash on board LED 
  delay(Dlay); 
  digitalWrite(OnBoardLED, HIGH); 
  delay(Dlay); 
} 

void forward(int amt, int lWheel, int rWheel) 
{ 
  analogWrite(6,lWheel); 
  analogWrite(5,rWheel); 
  digitalWrite(9,HIGH); // make left motor move forward 
  digitalWrite(10,LOW); 
  digitalWrite(11,HIGH); // make right motor move forward 
  digitalWrite(12,LOW); 
  delay(amt); 
  stopBot(500); 
} 

void stopBot(int pause) 
{ 
  digitalWrite(9,LOW); // stop both motors 
  digitalWrite(10,LOW); 
  digitalWrite(11,LOW); 
  digitalWrite(12,LOW); 
} 

void turnRight(int dlay,int lWheelSpeed) 
{ 
  analogWrite(6,lWheelSpeed); 
  digitalWrite(9,HIGH); // make the left motor move forward 
  digitalWrite(10,LOW); 
  digitalWrite(11,LOW); // stop the right motor 
  digitalWrite(12,LOW); 
  delay(dlay); 
}

Teensy: Square Pattern

Make your robot walk around a square pattern.

// Square Pattern 
// 6/6/19 
// Arduino 1.8.5 

// Set up pins 
const int OnBoardLED = 13; 
int Dlay = 500; 

void setup() 
{ 
  pinMode(9,OUTPUT); // set the pin modes to output 
  pinMode(10, OUTPUT); 
  pinMode(11,OUTPUT); 
  pinMode(12,OUTPUT); 
  pinMode(OnBoardLED,OUTPUT); 
} 

void loop() 
{ 
  blinky(); // blink the light 
  forward(1000); // move forward 
  stopBot(1000); // briefly pause 
  turnRight(1);  // turn at a right angle 
} 

void blinky() 
{ 
  digitalWrite(OnBoardLED, LOW); // flash on board LED 
  delay(Dlay); 
  digitalWrite(OnBoardLED, HIGH); 
  delay(Dlay); 
} 

void forward(int amt) 
{ 
  digitalWrite(9,HIGH); // make left motor move forward 
  digitalWrite(10,LOW); 
  digitalWrite(11,HIGH); // make right motor move forward 
  digitalWrite(12,LOW); 
  delay(amt); 
} 

void stopBot(int pause) 
{ 
  digitalWrite(9,LOW); // stop both motors 
  digitalWrite(10,LOW); 
  digitalWrite(11,LOW); 
  digitalWrite(12,LOW); 
} 

void turnRight(double dlay) 
{ 
  digitalWrite(9,HIGH); // make the left motor move forward 
  digitalWrite(10,LOW); 
  digitalWrite(11,LOW); // stop the right motor 
  digitalWrite(12,LOW); 
  delay(dlay); 
}

Teensy: Two Motors – forward

// Motor Driver 
// Darcy Chang 
// 6/6/19 
// Arduino 1.8.5 
const int 
OnBoardPin = 13; 
const int Dlay = 20; 

void setup() 
{ 
  // put your setup code here, to run once: 
  pinMode(9,OUTPUT); // set the pin modes to output 
  pinMode(10, OUTPUT); 
  pinMode(11,OUTPUT); 
  pinMode(12,OUTPUT); 
  pinMode(OnBoardPin,OUTPUT); 
} 

void loop() 
{ 
  // put your main code here to run repeatedly: 
  blinky(); 
  forward(); 
}
 
void blinky() 
{ 
  digitalWrite(OnBoardPin, LOW); // flash on board LED 
  delay(Dlay); 
  digitalWrite(OnBoardPin, HIGH); 
  delay(Dlay); 
} 

void forward() 
{ 
  digitalWrite(9, HIGH); // make left motor move forward 
  digitalWrite(10,LOW);
  digitalWrite(11,HIGH); // make right motor move forward 
  digitalWrite(12,LOW); 
} 
void stopBot() 
{ 
  digitalWrite(9,LOW); // stop both motors 
  digitalWrite(10,LOW); 
  digitalWrite(11,LOW); 
  digitalWrite(12,LOW); 
}

Teensy: Ultrasonic Sensor

This video will show you both how to install it (if it is not installed in yours or you want to add a 2nd one) and how to program it. Mr. Patton will also talk about how ultrasonic sensors work to detect objects.

Use pin # 14 for “trig” Use pin #15 for “echo” Once you get your ultrasonic sensor working, have it moving forward and when the robot detects an object at about 15 cm, it should avoid it by changing direction.

Ultrasonic Sensor – distance

https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

 

/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

Teensy: One Motor

// 1213MotorDriver

// L293D Motor Driver by Brian Patton

const int LeftA = 9;  // Left motor control pin A
const int LeftB = 10;  // Left motor control pin B

int Dlay = 1000;


void setup() {
  pinMode(LeftA, OUTPUT); // Set direction of the motor control pin
  pinMode(LeftB, OUTPUT); // Set direction of the motor control pin
}

void loop() {
  digitalWrite(LeftA, LOW);
  digitalWrite(LeftB, HIGH);

}