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 */ // 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 */ // 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); } }
Modify example so that on button press LED blinks 3 times.
Modify example so that on button press LED starts to blink with constant interval. Second press ends the blinking.
/* 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 */ // 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 */ // 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); }
Control LED on and off time with potentiometer.
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 */ #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 }
/* 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; }
Modify example so that text is center aligned.
Make a small animation with custom characters. Custom character code generator https://omerk.github.io/lcdchargen/
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); }
Find out which sensor sample time is faster.
/* 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); } }
/* 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); } }
Modify exampe so that robot drives number eight shape.
Make robot parallel park itself.