#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <Keypad.h>
const int displayWidth = 128;
const int displayHeight = 64;
int thick_edge = 500;
int slim_edge = 500;
int a = 100;
// int b = 1600 + a;
// int c = 120;
// int d = 200;
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 10, 9, 11 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { A0, A1, 2, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Adafruit_SSD1306 display(displayWidth, displayHeight, &Wire, -1);
void setup() {
Serial.begin(115200);
pinMode(A3, INPUT);
pinMode(A2, INPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, INPUT); // input for the button
pinMode(7, INPUT); // input for the button
pinMode(8, INPUT); // input for the button
pinMode(12, INPUT); // input for the button
pinMode(13, OUTPUT); // input for the button
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;) { // Don't proceed, halt the program
}
}
}
void loop() {
if(digitalRead(12)== HIGH){ // RUN MODE
display.clearDisplay();
display.setTextSize(1); // Set the text size for "HELLO RESMED"
display.setTextColor(WHITE); // Set text color to white
display.setCursor(38, 0); // Set starting position (x, y) for text
// Print "HELLO RESMED" character by character for better control
display.println("RUN MODE");
display.println("");
display.print("THICK edge time: ");
display.println(thick_edge);
display.println("");
display.print("SLIM edge time: ");
display.println(slim_edge);
display.println("");
display.print("THICK edge WIDTH: ");
display.print(a);
display.display();
delay(100);
Serial.println("WORKING IN RUN MODE");
if(digitalRead(8)== HIGH){
digitalWrite(13, HIGH);
//digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
for(int i = 0; i < a ; i = i+1){
digitalWrite(3, HIGH);
delayMicroseconds(thick_edge);
digitalWrite(3, LOW);
delayMicroseconds(thick_edge);
}
for(int i = a; i < 1600 ; i = i+1){
digitalWrite(3, HIGH);
delayMicroseconds(slim_edge);
digitalWrite(3, LOW);
delayMicroseconds(slim_edge);
}
for(int i = 1600 ; i < 1600 + a ; i = i+1){
digitalWrite(3, HIGH);
delayMicroseconds(thick_edge);
digitalWrite(3, LOW);
delayMicroseconds(thick_edge);
}
for(int i = 1600 + a; i < 3200 ; i = i+1){
digitalWrite(3, HIGH);
delayMicroseconds(slim_edge);
digitalWrite(3, LOW);
delayMicroseconds(slim_edge);
}
digitalWrite(13, LOW);
}
else if(digitalRead(7)== HIGH){
digitalWrite(2, HIGH);
while(digitalRead(4)!= HIGH){
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(3, LOW);
delayMicroseconds(500);
if(digitalRead(4)== HIGH){
break;
}
}
}
}
else if(digitalRead(12)== LOW){ // EDIT MODE
Serial.println("WORKING IN EDIT MODE");
display.clearDisplay();
display.setTextSize(1); // Set the text size for "HELLO RESMED"
display.setTextColor(WHITE); // Set text color to white
display.setCursor(38, 26); // Set starting position (x, y) for text
display.println("EDIT MODE");
display.display();
delay(100);
char key = keypad.getKey();
Serial.println(key);
if (key == '#'){
Serial.println(key);
Serial.print("Edge timing editing");
while(true){
char key = keypad.getKey();
analogRead(A2);
analogRead(A3);
thick_edge = map(analogRead(A2), 0 , 1023, 500, 800);
slim_edge = map(analogRead(A3), 0 , 1023, 200, 500);
display.clearDisplay();
display.setTextSize(1); // Set the text size for "HELLO RESMED"
display.setTextColor(WHITE); // Set text color to white
display.setCursor(38, 0); // Set starting position (x, y) for text
display.println("EDIT MODE");
display.println("");
display.println("");
display.print("THICK edge time: ");
display.println(thick_edge);
display.println("");
display.print("SLIM edge time: ");
display.print(slim_edge);
display.display();
delay(100);
if(key == '#'){
break;
}
}
}
else if (key == '*') {
Serial.println(key);
Serial.println("Edge thickness editing");
while(true){
Serial.print("In Loop");
display.clearDisplay(); // Clear display before printing new number
display.setCursor(0, 10); // Set cursor position
display.print("Thick Seam Width: ");
display.display(); // Update the display
delay(100); // Delay for stability
Serial.print(key); // Echo the key press
a = getNumberFromKeypad();
break;
}
}
}
}
int getNumberFromKeypad() {
int num = 0;
char key;
display.clearDisplay(); // Clear the display before entering the loop
display.setTextSize(1); // Set text size
display.setTextColor(WHITE); // Set text color
while (true) {
key = keypad.getKey();
if (key != NO_KEY) {
if (key == '*') { // Assume '#' is used to indicate end of input
break;
}
if (key >= '0' && key <= '9') {
num = num * 10 + (key - '0'); // Shift left and add new digit
if (num > 600) {
num = 600; // Limit to 600
}
display.clearDisplay(); // Clear display before printing new number
display.setCursor(0, 10); // Set cursor position
display.print("Thick Seam Width: ");
display.println(num); // Print the number on the display
display.display(); // Update the display
delay(100); // Delay for stability
Serial.print(key); // Echo the key press
}
}
}
display.clearDisplay(); // Clear display after input is completed
display.display(); // Update the display
Serial.println(); // New line after input is completed
return num;
}