Last active
April 28, 2019 13:34
-
-
Save BarrYPL/462761d8b7185e50f355e9a05b3b79ff to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <Wire.h> | |
//Tablice na argumenty | |
char arg1[20]; | |
char arg2[20]; | |
char arg3[20]; | |
char inChar; | |
void setup() { | |
Serial.begin(9600); | |
Wire.begin(); | |
while (!Serial); | |
Serial.println("Serial: Rozpoczęto - podaj dane: "); | |
} | |
void loop() { | |
int flag = 0; | |
int index1 = 0; | |
int index2 = 0; | |
int index3 = 0; | |
while (Serial.available() > 0) { | |
inChar = Serial.read(); | |
if (inChar == ',') { | |
flag++; | |
} | |
if (flag == 0) { | |
arg1[index1] = inChar; | |
index1++; | |
arg1[index1] = '\0'; | |
} | |
if ((flag == 1) && (index1 == 0)) { | |
arg2[index2] = inChar; | |
index2++; | |
arg2[index2] = '\0'; | |
} | |
if ((flag == 2) && (index2 == 0)) { | |
arg3[index3] = inChar; | |
index3++; | |
arg3[index3] = '\0'; | |
} | |
//If z ustawieniami indexów i flagi sprawia, że argumenty się nie powtarzają | |
if (flag == 1) { | |
index1 = 0; | |
} | |
if (flag == 2) { | |
index2 = 0; | |
} | |
} | |
//Zamiana argumentów na liczby. | |
int argI = atoi(arg1); | |
int argII = atoi(arg2); | |
int argIII = atoi(arg3); | |
//jeśli ostatni argument to 1 to wyśli, bo będę dopisywał więcej rzeczy dla siebie | |
if (argIII == 1) { | |
SendToI2C(argI, argII); | |
} | |
//Wyświetl argumenty jesli potrzebujesz | |
Serial.print(argI); | |
Serial.print("\t"); | |
Serial.print(argII); | |
Serial.print("\t"); | |
Serial.print(argIII); | |
Serial.println(" "); | |
//Wyczyść bufor i czekaj na dane | |
Serial.flush(); | |
while (!Serial.available()); | |
} | |
//argument a zostanie wysłany do urządzenia o adresie b. | |
void SendToI2C(int a, int b){ | |
int x; | |
Wire.beginTransmission(b) | |
Wire.write(a); | |
x = Wire.endTransmission(); | |
if (x != 0){ | |
Serial.println("Wystapil blad komunikacji: "); | |
Serial.println(x); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment