Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include <String.h>
- //stepper
- #define DIR 6
- #define STEP 7
- #define ANGLE 1.8
- const float STEPS_PER_REVOLUTION = 360 / ANGLE;
- int speeed = 25;
- //
- //control
- #define X A0
- #define Y A1
- #define BTN 10
- //
- //Define the LCD Screen
- const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- //
- //presets
- const int preset_count = 6;
- struct pair {
- int F;
- int C;
- char n[20];
- } presets[preset_count];
- char* names[preset_count] = {"Nou-nascut", "Copil", "Adolescent", "Adult", "Varstnic", "Custom"};
- int Fs[preset_count] = {1, 2, 3, 4, 5, 3};
- int Cs[preset_count] = {1, 2, 3, 4, 5, 3};
- //
- int cPreset = 0;
- int stage = 0;
- int selection = 0;
- void setup() {
- Serial.begin(9600);
- //stepper
- pinMode(STEP, OUTPUT);
- pinMode(DIR, OUTPUT);
- //Control
- pinMode(BTN, INPUT);
- digitalWrite(BTN, HIGH);
- //presets
- for (int i = 0; i < preset_count; i++) {
- strcpy(presets[i].n, names[i]);
- presets[i].F = Fs[i];
- presets[i].C = Cs[i];
- }
- //lcd
- lcd.begin(16, 2);
- lcd.print("OxiGeniuine v1.0");
- delay(2000);
- updateDisplay();
- }
- void loop() {
- int btn = buttonPressed(analogRead(X), analogRead(Y), digitalRead(BTN));
- if (stage == 0) {
- if (btn != -1) {
- switch (btn) {
- case 1:
- cPreset--;
- if (cPreset < 0) {
- cPreset = preset_count - 1;
- }
- updateDisplay();
- break;
- case 4:
- cPreset++;
- if (cPreset > preset_count - 1) {
- cPreset = 0;
- }
- updateDisplay();
- break;
- case 5:
- stage++;
- updateDisplay();
- break;
- }
- }
- } else if (stage == 1) {
- switch (btn) {
- case 1:
- selection = 0;
- break;
- case 4:
- selection = 1;
- break;
- case 5:
- switch (selection) {
- case 0:
- break;
- case 1:
- break;
- }
- break;
- }
- delay(300);
- } else if (stage == 2) {
- if (btn == 3) {
- if (!changingValue)
- if (++selection > 3) selection = 0;
- updateDisplay();
- delay(300);
- } else if (btn == 2) {
- if (!changingValue)
- if (--selection < 0) selection = 3;
- updateDisplay();
- delay(300);
- } else if (btn == 5) {
- if (selection == 0) {
- changingValue = !changingValue;
- } else if (selection == 1) {
- changingValue = !changingValue;
- } else if (selection == 2) {
- if (!changingValue) stage--;
- } else if (selection == 3) {
- //TODO: Start The motor
- }
- delay(300);
- }
- }
- }
- void updateDisplay() {
- if (stage == 0) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Programe:");
- lcd.setCursor(2, 1);
- lcd.print(presets[cPreset].n);
- } else if (stage == 1) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(presets[cPreset].n);
- lcd.setCursor(12, 0);
- lcd.print("| <-");
- lcd.setCursor(0, 1);
- lcd.print("F: | C: | ->");
- lcd.setCursor(3, 1);
- lcd.print(presets[cPreset].F);
- lcd.setCursor(10, 1);
- lcd.print(presets[cPreset].C);
- lcd.blink();
- lcd.cursor();
- if (selection == 0) {
- lcd.setCursor(13, 0);
- } else if (selection == 1) {
- lcd .setCursor(13, 1);
- }
- } else if (stage == 2 || stage == 3) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(presets[cPreset].n);
- lcd.setCursor(12, 0);
- lcd.print("| <-");
- lcd.setCursor(0, 1);
- lcd.print("F: | C: | ->");
- lcd.setCursor(3, 1);
- lcd.print(presets[cPreset].F);
- lcd.setCursor(10, 1);
- lcd.print(presets[cPreset].C);
- if (selection == 0) {
- lcd.setCursor(3, 1);
- } else if (selection == 1) {
- lcd.setCursor(10, 1);
- } else if (selection == 2) {
- lcd.setCursor(13, 0);
- } else if (selection == 3) {
- lcd .setCursor(13, 1);
- }
- }
- }
- //Based on global variables X Y BTN, returns a control option from 1 to 5, -1 being no command. acc is used to set the threshold for a joystick action
- float acc = 400;
- int buttonPressed(int x, int y, bool btn) {
- if (x - 500 > acc)
- return 4;
- else if ((-1) * (x - 500) > acc)
- return 1;
- else if (y - 500 > acc)
- return 3;
- else if ((-1) * (y - 500) > acc)
- return 2;
- else if (btn == false)
- return 5;
- return -1;
- }
- /**
- Turn the stepper for steps, in CW direction if dir is 1 and CCW if dir is 0
- */
- void steps(int steps, int dir) {
- dir == 1 ? digitalWrite(DIR, HIGH) : digitalWrite(DIR, LOW);
- for (int i = 0; i < steps; i++) {
- digitalWrite(STEP, HIGH);
- delayMicroseconds(speeed);
- digitalWrite(STEP, LOW);
- delayMicroseconds(speeed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement