/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-toggle-led
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// constants won't change
const int BUTTON_PIN = 2; // Arduino pin connected to button's pin
const int LED_PIN = 3; // Arduino pin connected to LED's pin
// variables will change:
int ledState = LOW; // the current state of LED
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup() {
Serial.begin(9600); // initialize serial
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(200); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(3); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(18, 10); // position to display
oled.println("F.A.I"); // text to display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(25, 35); // position to display
oled.println("SMArT System"); // text to display
oled.display(); // show on OLED
pinMode(BUTTON_PIN, INPUT); // set arduino pin to input pull-up mode
pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
currentButtonState = digitalRead(BUTTON_PIN);
}
void hit(int bawah, int atas, int pin, int durasi)
{ //setup fungsi hit + parameter yang digunakan
for(int i = bawah; i < atas; i++)
{
digitalWrite(pin, HIGH);
delay(durasi);
digitalWrite(pin, LOW);
delay(durasi);
}
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
oled.clearDisplay();
oled.setTextSize(3);
oled.setTextColor(WHITE);
oled.setCursor(10, 20);
oled.println("A1 HIT");
oled.display();
// toggle state of LED
ledState = !ledState;
// Kontrol LED sesuai status toggle
digitalWrite(LED_PIN, ledState);
hit(1, 5, LED_PIN, 200);
}
}