Här är hela koden.
Kod: Markera allt
const int controlPin1 = 7;
const int controlPin2 = 6;
const int enablePin = 9;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
int sensor01;
int sensor02;
int stepOld;
int step;
long PulseCount;
long UserStopPulseCount;
int intest1Pin = 4;
int intest2Pin = 5;
int intest3Pin = 10;
int intest4Pin = 11;
int intest5Pin = 16;
int intest6Pin = 17;
int stopsensor1Pin = 12;
int stopsensor2Pin = 8;
int stopsensor3Pin = 15;
int val = 0;
int tonePin = 13;
float f = 0.00f;
int eeAddress = 0;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#define SENSOR_A 3
#define SENSOR_B 2
void checkState()
{
sensor01 = digitalRead(SENSOR_A);
sensor02 = digitalRead(SENSOR_B);
if (sensor01 == 1 && sensor02 == 1) {
step = 0;
if (stepOld == 1) {
PulseCount--;
}
if (stepOld == 3) {
PulseCount++;
}
stepOld = 0;
}
if (sensor01 == 0 && sensor02 == 1) {
step = 1;
if (stepOld == 2) {
PulseCount--;
}
if (stepOld == 0) {
PulseCount++;
}
stepOld = 1;
}
if (sensor01 == 0 && sensor02 == 0) {
step = 2;
if (stepOld == 3) {
PulseCount--;
}
if (stepOld == 1) {
PulseCount++;
}
stepOld = 2;
}
if (sensor01 == 1 && sensor02 == 0) {
step = 3;
if (stepOld == 0) {
PulseCount--;
}
if (stepOld == 2) {
PulseCount++;
}
stepOld = 3;
}
}
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
attachInterrupt(0, checkState, CHANGE);
attachInterrupt(1, checkState, CHANGE);
PulseCount = 0;
EEPROM.get(0, UserStopPulseCount);
pinMode(intest1Pin, INPUT);
pinMode(intest2Pin, INPUT);
pinMode(intest3Pin, INPUT);
pinMode(intest4Pin, INPUT);
pinMode(intest5Pin, INPUT);
pinMode(intest6Pin, INPUT);
pinMode(stopsensor1Pin, INPUT);
pinMode(stopsensor2Pin, INPUT);
pinMode(stopsensor3Pin, INPUT);
pinMode(tonePin, OUTPUT);
{
lcd.setCursor(2, 0);
lcd.print("PROTOTYPE");
Serial.println("Boot initialized");
delay(500);
lcd.setCursor(11, 1);
lcd.print("1");
delay(100);
lcd.setCursor(11, 1);
lcd.print("10");
delay(100);
lcd.setCursor(11, 1);
lcd.print("100");
delay(900);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Prototype 1.0");
delay(200);
lcd.setCursor(0, 1);
lcd.print("www.");
delay(100);
lcd.setCursor(12, 1);
lcd.print(".se");
delay(100);
lcd.setCursor(0, 1);
lcd.print("www.PROTOTYPE.se");
delay(250);
lcd.setCursor(0, 1);
lcd.print("www.PROTOTYPE.se");
Serial.println("unit is active");
delay(800);
lcd.clear();
}
}
void loop()
{
lcd.setCursor(0, 0);
lcd.print("Pulses:");
if (PulseCount > -1) {
lcd.print(" ");
}
if (PulseCount < 10 && PulseCount > -10) {
lcd.print(" ");
}
if (PulseCount < 100 && PulseCount > -100) {
lcd.print(" ");
}
lcd.print(PulseCount);
if (sensor01 == 1) {
lcd.print(" HIGH");
}
else {
lcd.print(" LOW ");
}
lcd.setCursor(0, 1);
if (sensor02 == 1) {
lcd.print(" HIGH");
}
else {
lcd.print(" LOW ");
}
{
val = digitalRead(intest3Pin);
if (val == HIGH) {
UserStopPulseCount = PulseCount;
EEPROM.put(0, UserStopPulseCount);
lcd.setCursor(0, 1);
lcd.print(PulseCount);
delay(750);
lcd.setCursor(0, 1);
lcd.print("Pos. SAVED");
Serial.println("Saving liftposition to memory");
delay(750);
}
}
{
val = digitalRead(intest4Pin);
if (val == HIGH) {
UserStopPulseCount = 0;
EEPROM.put(0, UserStopPulseCount);
lcd.setCursor(0, 1);
lcd.print("Pos. erased");
Serial.println("Erasing position from memory");
delay(1000);
}
}
{
if ( UserStopPulseCount != 0 && PulseCount >= UserStopPulseCount - 15 && PulseCount <= UserStopPulseCount + 15 )
{
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, LOW);
lcd.setCursor(0, 1);
lcd.print("Users Stop");
Serial.println("Users programmed STOP activated");
delay(1500);
}
}
{
val = digitalRead(intest5Pin);
if (val == HIGH) {
lcd.setCursor(0, 1);
lcd.print("Button 5");
Serial.println("Playing tone on button 5!");
delay(200);
tone(tonePin, 600);
delay(50);
tone(tonePin, 950);
delay(70);
tone(tonePin, 680, 125);
delay(100);
tone(tonePin, 600);
delay(50);
tone(tonePin, 950);
delay(70);
tone(tonePin, 720, 125);
delay(800);
noTone(tonePin);
delay(100);
}
}
{
val = digitalRead(intest6Pin);
if (val == HIGH) {
lcd.setCursor(0, 1);
lcd.print("Button 6");
Serial.println("Playing tone on button 6!");
delay(200);
tone(tonePin, 600);
delay(50);
tone(tonePin, 720);
delay(70);
tone(tonePin, 680);
delay(100);
tone(tonePin, 600);
delay(50);
tone(tonePin, 950);
delay(70);
tone(tonePin, 880, 125);
delay(800);
noTone(tonePin);
delay(100);
}
}
{
val = digitalRead(intest1Pin);
if (val == HIGH)
{
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
{
val = digitalRead(stopsensor3Pin);
if (val == HIGH) {
digitalWrite(controlPin1, LOW);
lcd.setCursor(0, 1);
lcd.print("User Stop");
Serial.println("User removeable module defined stop 1");
delay(50);
tone(tonePin, 600);
delay(50);
tone(tonePin, 950);
delay(70);
tone(tonePin, 800, 125);
delay(800);
noTone(tonePin);
}
}
{
val = digitalRead(stopsensor2Pin);
if (val == HIGH) {
digitalWrite(controlPin1, LOW);
lcd.setCursor(0, 1);
lcd.print("Up Stop");
Serial.println("Upper security sensor reached Can only go down");
delay(50);
tone(tonePin, 880);
delay(125);
tone(tonePin, 400, 125);
delay(650);
noTone(tonePin);
}
lcd.setCursor(0, 1);
lcd.print("Up");
Serial.println("Lift going up at full speed");
delay(50);
}
}
else {
val = digitalRead(intest2Pin);
if (val == HIGH) {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
{
val = digitalRead(stopsensor3Pin);
if (val == HIGH) {
digitalWrite(controlPin2, LOW);
lcd.setCursor(0, 1);
lcd.print("User Stop");
Serial.println("User removeable module defined stop 2");
delay(50);
tone(tonePin, 600);
delay(50);
tone(tonePin, 950);
delay(70);
tone(tonePin, 800, 125);
delay(800);
noTone(tonePin);
}
}
{
val = digitalRead(stopsensor1Pin);
if (val == HIGH) {
digitalWrite(controlPin2, LOW);
lcd.setCursor(0, 1);
lcd.print("Down Stop");
Serial.println("Lower security sensor reached Can only go up");
delay(50);
tone(tonePin, 880);
delay(125);
tone(tonePin, 400, 125);
delay(650);
noTone(tonePin);
}
lcd.setCursor(0, 1);
lcd.print("Down");
Serial.println("Lift going down at full speed");
delay(50);
}
} else
{
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, LOW);
}
}
}
}