TTU Summer School programming

Project 1 Controlling LED with button

Arduino ühendusskeem

Example #1.1 LED light up when button is pressed

Theory: https://www.arduino.cc/en/Tutorial/DigitalPins.

/*
Example #1.1
*/
 
/* Constants */
// Set button pin
const int button = A0; 
 
// Set LED pin
const int led = 13; 
 
/* Global variables */
// Holds the current state of button
int buttonState = 0; 
 
void setup() 
{ 
  // Set LED pin to output
  pinMode(led, OUTPUT); 
  // Set button pin to input with internal pull-up activated
  pinMode(button, INPUT_PULLUP); 
}
 
void loop()
{
  // Get the curren state of button pin
  buttonState = digitalRead(button); 
  // If button is pressed then turn on LED
  if (buttonState == LOW) 
  { 
    digitalWrite(led, HIGH);
  } 
  // All other cases turn LED off
  else 
  { 
   digitalWrite(led, LOW);
  }
}

Example #1.2 Short push on the button makes LED light up for 1 second

/*
Example #1.2
*/
 
// Setup is same with example #1.1
const int button = A0; 
const int led = 13; 
 
int buttonState = 0; 
 
void setup() 
{ 
  pinMode(led, OUTPUT); 
  pinMode(button, INPUT_PULLUP); 
}
 
void loop()
{
  //  Get the curren state of button pin
  buttonState = digitalRead(button); 
 
  // When button is pressed then set LED on and halt program for 1 second
  if (buttonState == LOW) 
  { 
    digitalWrite(led, HIGH);
    delay(1000); // 1000 millisekundit ehk 1 sekund
  }
  // Turn LED off
  digitalWrite(led, LOW);
}

Example #1.3 Short push on the button changes LED state

/*
Example #1.3
*/
 
// Setup is same with example #1.1
const int button = A0; 
const int led = 13; 
 
int buttonState = 0; 
 
void setup() 
{ 
  pinMode(led, OUTPUT); 
  pinMode(button, INPUT_PULLUP); 
}
 
void loop() 
{
  // When button is pressed and hold the program stays inside while(1) loop
  if (digitalRead(button) == LOW) 
    {
   // Debounce effect eliminator delay
    delay(50); 
    while (digitalRead(button) == LOW)
     { 
     }
	// Invert LED current state
    digitalWrite(led, !digitalRead(led)); 
 
   // Debounce effect eliminator delay
    delay(50); 
    }
}

Exercises

Exercise #1.1

Modify example so that on button press LED blinks 3 times.

Exercise #1.2

Modify example so that on button press LED starts to blink with constant interval. Second press ends the blinking.

Project 2 Controlling LED with potentiometer

Example #2.1 Potentiometer controls state of LED

/*
Example #2.1
*/
 
// Set potentiometer pin
const int pot = A1; 
 
// Set LED pin
const int led = 13; 
 
// Holds potentiometer digital value
int potState = 0; 
 
void setup()
{
  // Set LED pin to output
  pinMode(led, OUTPUT);
}
 
void loop() 
{
  // Get digital value of potentiometer
  potState = digitalRead(pot);
 
  // When potentiometer digital value is logical "true" then turn on LED
  if(potState > 0)
  {
    digitalWrite(led, HIGH);
  }
  // All other cases set LED off
  else
  {
    digitalWrite(led, LOW);
  }
}

Example #2.2 Controlling LED blinking frequency with potentiometer

/* 
Example #2.2
*/
 
// Setup is same with example #2.1
const int pot = A1; 
const int led = 13; 
 
int potState = 0; 
 
void setup()
{
  pinMode(led, OUTPUT);
}
 
void loop() 
{
  // Get analog value of potentiometer
  potState = analogRead(pot); 
 
  // Turn LED on
  digitalWrite(led, HIGH); 
 
  // Halt the program for potentiometer analog value number of milliseconds
  delay(potState);  
 
  // Turn LED off
  digitalWrite(led, LOW); 
 
  // Halt the program for potentiometer analog value number of milliseconds
  delay(potState); 
}

Example #2.3 Controlling LED brightness with potentiometer

/*
Example #2.3
*/
// Setup is same with example #2.1
 
const int pot = A1; 
const int led = 13; 
int potState = 0; 
 
void setup()
{
  pinMode(led, OUTPUT);
}
 
void loop() 
{
  // Get analog value of potentiometer
  potState = analogRead(pot); 
 
  // When potentiometer value is greater than 0 then set LED on halt program for short period
  if (potState > 0) 
  {
    digitalWrite(led, HIGH);
    delayMicroseconds(potState); 
  }
 
  // Turn LED off
  digitalWrite(led, LOW); 
 
  // Halt program for short period
  delayMicroseconds(1023 - potState); 
}

Exercises

Exercise #2.2

Control LED on and off time with potentiometer.

Project 3 Alphabetical LCD

Example #3.1 Writing on LCD screen

Theory: https://www.arduino.cc/en/Reference/LiquidCrystal

/*
Example #3.1
*/
// Include LCD library
#include <LiquidCrystal.h> 
 
// Create LCD object and set hardware connection pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
void setup() 
{
  // Define LCD size
  lcd.begin(16, 2); 
 
  // Print out welcome message
  lcd.print("Hello World!"); 
}
 
void loop() 
{
  // Change cursor position to second line
  lcd.setCursor(0, 1); 
 
  // Print out program working time in seconds
  lcd.print(millis()/1000); 
}

Example #3.2 Custom characters on LCD

/*
Example #3.2
*/
 
#include <LiquidCrystal.h>
 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
  // Array to generate new character,
  // "0b" in front of the number tells the compiler that it's a binary number
byte customChar[8] = 
{
  0b11111,
  0b00000,
  0b01010,
  0b00000,
  0b10001,
  0b01110,
  0b00000,
  0b11111
};
 
void setup()
{
  // Load new character to LCD memory position "0"
  lcd.createChar(0, customChar); 
 
  // Define LCD size
  lcd.begin(16, 2); 
 
  // Display new custom character on LCD
  lcd.write((uint8_t)0);
}
void loop()
{
  // Do nothing
}

Example #3.3 Reading LCD shield buttons

/*
Nimetus: Example #3.3
Kirjeldus: Reading LCD shield buttons
*/
 
#include <LiquidCrystal.h>
 
 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
// Define button states constants
const int RIGHT = 0;
const int UP    = 1;
const int DOWN  = 2;
const int LEFT  = 3;
const int SELECT = 4;
const int NONE  = 5;
 
/* Global variables */
// Holds the value of pressed button
int pressedButton;
// Holds the value of analog input
int buttonValue;
 
void setup() 
{
  // Define LCD size
  lcd.begin(16, 2); 
 
  // Print explaining text
  lcd.print("ADC: ");
 
  // Print out analog input value
  lcd.print(analogRead(A0)); 
}
void loop() 
{
  // Calls out fucntion and saves returned value to variable
  pressedButton = checkButtons(); 
 
  // Check if there is a pressed button
  if(pressedButton < NONE)
  {
    lcd.clear(); // Clear LCD
 
    lcd.print("ADC: "); // Print out explaining text
 
    lcd.print(buttonValue); // Print out analog input value
 
    delay(500); // Halts program for 500ms to get stable text on LCD
  }
}
// Function read analog input value and compares it with values of button states
// Tagastab arvu vahemikus 0 kuni 5 vastavalt defineeritud konstandile
int checkButtons()
{
 // Get analog input value
 buttonValue = analogRead(A0);
 
 if (buttonValue < 50)   return RIGHT;
 if (buttonValue < 195)  return UP;
 if (buttonValue < 380)  return DOWN;
 if (buttonValue < 555)  return LEFT;
 if (buttonValue < 790)  return SELECT;
 // If no button state matches then return NONE
 return NONE;
}

Exercises

Exercise #3.1

Modify example so that text is center aligned.

Exercise #3.2

Make a small animation with custom characters. Custom character code generator https://omerk.github.io/lcdchargen/

Project 4 Reading sensors and displaying results on LCD

Example #4.2 Distance and proximity sensors reading

Libraries: https://www.arduino.cc/en/guide/libraries#toc4

/*
Example #4.2 
*/
 
// Include libraries
#include <LiquidCrystal.h> 
#include <NewPing.h> 
 
// Set pins for sensors
const int US_TRIGGER = A2;
const int US_ECHO = A3;
const int IR = A4;
 
/* Global variables and constants */
const int maxDistance = 200;
int distance, proximity;
 
// Create LCD object and define connection pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
// Create ultrasonic object and define connection pins
NewPing sonar(US_TRIGGER, US_ECHO,maxDistance);
 
void setup() 
{
  //Define LCD size
  lcd.begin(16, 2); 
 
  // Print out explaining text
  lcd.print("Dist:");
  lcd.setCursor(0, 1);
  lcd.print("Object:");
}
void loop() 
{
  // Get distance in centimeters
  distance = sonar.ping_cm();
 
  // Get proximity of object
  proximity = digitalRead(IR); 
 
  // Set cursor to first line 10th character
  lcd.setCursor(9, 0);
 
  // EPrint out distance
  lcd.print(distance); 
 
// Overwrite previous value numbers with spaces
  lcd.print("   "); 
 
  // Set cursor on second line 10th character
  lcd.setCursor(9, 1); 
 
  // when infrared sensor does not see anything print out "no", otherwise print out "yes".
  if(proximity == 1) lcd.print("no "); 
  else lcd.print("yes"); 
 
  // Halt program to get stable view of LCD 
  delay(500); 
}

Exercises

Exercise #4.1

Find out which sensor sample time is faster.

Project 5 DC motor drive and speed control

Example #5.1 DC motor ad H-bridge

/*
Example #5.1
*/
// Set pins for driver inputs
const int left_A = 10; // driver pin A-1A
const int left_B = 12; // driver pin A-1B
const int right_A = 11; // driver pin B-1A
const int right_B = 13; // driver pin B-2A
 
void setup() 
{
  // Set driver control pins as output
  pinMode(left_A,OUTPUT);
  pinMode(left_B,OUTPUT);
  pinMode(right_A,OUTPUT);
  pinMode(right_B,OUTPUT);
 
  motors(0,0); // stop motors
  delay(2000); // halt program for safety purposes 2 seconds
}
 
void loop() 
{
 
  motors(1,1); // robot goes forward
  delay(2000); // halt program for 2 seconds
 
  motors(-1,1); // robot turns left
  delay(500);  // halt program for 500 milliseconds
}
 
// This function controls the driver input pins to get correct motion
void motors(int left, int right)
{
  // Left Motor
  if(left == 1)
  { // Left motor CW
    digitalWrite(left_A,HIGH);
    digitalWrite(left_B,LOW);
  }
  else if(left == -1)
  { // Left motor CCW
    digitalWrite(left_A,LOW);
    digitalWrite(left_B,HIGH);
  }
  else
  { // Left motor STOP
    digitalWrite(left_A,LOW);
    digitalWrite(left_B,LOW);
  }
 
  // Right motor
  if(right == 1)
  { // Right motor CW
    digitalWrite(right_A,HIGH);
    digitalWrite(right_B,LOW);
  }
  else if(right == -1)
  { // Right motor CCW
    digitalWrite(right_A,LOW);
    digitalWrite(right_B,HIGH);
  }
  else
  { // Right motor STOP
    digitalWrite(right_A,LOW);
    digitalWrite(right_B,LOW);
  }
}

Example #5.2 DC motor speed controlling

/* Nimetus: 
Example #5.2
*/
// Set pins for driver inputs
const int left_A = 10; // driver pin A-1A
const int left_B = 12; // driver pin A-1B
const int right_A = 11; // driver pin B-1A
const int right_B = 13; // driver pin B-2B
 
void setup() 
{
  // Set driver control pins as output
  pinMode(left_A,OUTPUT);
  pinMode(left_B,OUTPUT);
  pinMode(right_A,OUTPUT);
  pinMode(right_B,OUTPUT);
 
  motors(0,0); // Stop motors
  delay(2000); // Delay for safety purposes
}
void loop()
 {
  motors(100,100); // go fwd
  delay(2000);
  motors(100,255); // turn left
  delay(2000); 
  motors(-255,-255); // go back
  delay(2000);
  motors(-255,-100); // go backward while turning right
  delay(2000); 
  motors(150,-150); // turn on spot
  delay(2000);
}
 
// This function sets the motors speed and directions
void motors(int left, int right)
 {
  // Left motor
  if(left > 0 && left <= 255)
  { // Left motor CW
    analogWrite(left_A,left);
    digitalWrite(left_B,LOW);
  }
  else if(left < 0 && left >= -255)
  { // Left motor CCW
    analogWrite(left_A, 255+left);
    digitalWrite(left_B,HIGH);
  }
  else
  { // Left motor STOP
    digitalWrite(left_A,LOW);
    digitalWrite(left_B,LOW);
  }
 
  // Right motor
  if(right > 0 && right <= 255)
  { // Right motor CW
    analogWrite(right_A,right);
    digitalWrite(right_B,LOW);
  }
  else if(right < 0 && right >= -255)
  { // Right motor CCW
    analogWrite(right_A,255+right);
    digitalWrite(right_B,HIGH);
  }
  else
  { // Right motor STOP
    digitalWrite(right_A,LOW);
    digitalWrite(right_B,LOW);
  }
}

Exercises

Exercise #5.1

Modify exampe so that robot drives number eight shape.

Harjutus #5.2

Make robot parallel park itself.

en/arduino/examples.txt · Last modified: 2020/07/20 09:00 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0