Last active
March 13, 2017 10:25
-
-
Save Dubiy/f5da5b507963d0d9aec3d1dd97947244 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
SoftwareSerial mySerial(10, 11); // RX, TX | |
byte ledState = 0, | |
incomingByte = 0, | |
equalsCount = 0, | |
weight[6] = {0,0,0,0,0,0}; | |
char lastValue[10], | |
currentValue[10]; | |
void setup() { | |
Serial.begin(4800, SERIAL_8E1); | |
// initialize digital pin LED_BUILTIN as an output. | |
pinMode(LED_BUILTIN, OUTPUT); | |
mySerial.begin(2400); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
// send data only when you receive data: | |
if (mySerial.available()) { | |
if (mySerial.read() == '=') { | |
byte i = 0; | |
strcpy(currentValue, ""); | |
while (i < 8) { | |
if (mySerial.available()) { | |
currentValue[i] = mySerial.read(); | |
// Serial.print(currentValue[i]); | |
i++; | |
} | |
} | |
currentValue[i] = 0; | |
if (strcmp(currentValue, lastValue) == 0) { | |
if (equalsCount < 255) { | |
equalsCount++; | |
} | |
// Serial.print("+"); | |
} else { | |
equalsCount = 0; | |
// Serial.println("upd"); | |
strcpy(lastValue, currentValue); | |
} | |
// Serial.println(currentValue); | |
mySerial.flush(); | |
} | |
} | |
if (Serial.available() > 0) { | |
incomingByte = Serial.read(); | |
ledState = -ledState + 1; | |
switch (incomingByte) { | |
case 1: { | |
//тарування | |
} break; | |
case 2: { | |
//передачі ціни | |
} break; | |
case 3: { | |
//запит ваги | |
for (byte i = 0; i < 6; i++) { | |
weight[i] = 0; | |
} | |
if (currentValue[7] == '-') { | |
break; | |
} | |
if (equalsCount > 10) { | |
for (byte i = 0; i < 4; i++) { | |
if (currentValue[i] == '.') { | |
//do some magic cuz decimal point may float | |
int j; | |
for (j = i - 1; j >= 0; j--) { | |
weight[2 - (i - 1 - j)] = currentValue[j] - 0x30; | |
} | |
for (j = i + 1; j < 7; j++) { | |
if (currentValue[j] > 0) { | |
weight[3 + j - (i + 1)] = currentValue[j] - 0x30; | |
} | |
} | |
for (j = 0; j < 6; j++) { | |
Serial.write(weight[j]); | |
} | |
for (j = 0; j < 12; j++) { | |
Serial.write(0); | |
} | |
break; | |
} | |
} | |
} | |
} break; | |
case 4: { | |
//повертаю вагу, що прийшла | |
Serial.println(currentValue); | |
} break; | |
} | |
} | |
digitalWrite(LED_BUILTIN, ledState); | |
} |
Author
Dubiy
commented
Mar 12, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment