3 integers over serial
11 berichten
• Pagina 1 van 2 • 1, 2
- Nightmystic
- Berichten: 133
- Geregistreerd: 10 Jan 2016, 16:39
3 integers over serial
Ik wil graag drie integers via serial versturen van een Arduino Mega naar een andere Arduino Mega, maar het lukt niet.
Wie wil me helpen?
Arduino Mega die zend:
cpp code
Arduino Mega die ontvangt
cpp code
Wie wil me helpen?
Arduino Mega die zend:
cpp code
int R;
int G;
int B;
char buffer[10];
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}
void loop()
{
R = R + 1;
G = G - 10;
B = B + 10;
if (R > 254)
{
R = 0;
}
if (G < 20)
{
G = 200;
}
if (B > 200)
{
B = 0;
}
Serial.print(R);Serial.print(" | "); Serial.print(G);Serial.print(" | ");Serial.println(B);
sprintf(buffer,"%03d%03d%03d",R,G,B);
Serial1.println(buffer);
delay(100);
}
Arduino Mega die ontvangt
cpp code
String readString, R, G, B;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}
void loop()
{
while (Serial1.available())
{
delay(10);
if (Serial1.available() >0)
{
char c = Serial1.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0)
{
Serial.println(readString); //see what was received
R = readString.substring(0, 4); //get the first four characters
G = readString.substring(4, 8); //get the next four characters
B = readString.substring(8, 12); //get the last four characters
int n1; //declare as number
int n2;
int n3;
char carray1[6]; //magic needed to convert string to a number
R.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
G.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2);
char carray3[6];
B.toCharArray(carray3, sizeof(carray3));
n3 = atoi(carray3);
Serial.print("R "); Serial.print(n1); Serial.print(" | ");
Serial.print("G "); Serial.print(n2); Serial.print(" | ");
Serial.print("B "); Serial.println(n3);
readString="";
delay(200);
}
}
Advertisement
- nicoverduin
- Berichten: 5043
- Geregistreerd: 13 Mei 2013, 20:57
- Woonplaats: Heemskerk
Re: 3 integers over serial
Wat zou nu mijn vraag zijn...........???????
- Nightmystic
- Berichten: 133
- Geregistreerd: 10 Jan 2016, 16:39
Re: 3 integers over serial
Ik durf het niet te zeggen Nico, wat ga je me vragen?
Hahaha volgens mij of ik kan tellen
Hahaha volgens mij of ik kan tellen
- nicoverduin
- Berichten: 5043
- Geregistreerd: 13 Mei 2013, 20:57
- Woonplaats: Heemskerk
Re: 3 integers over serial
"dokter.... het wil niet lukken...." Wat zou zijn eerste vraag zijn?
- Nightmystic
- Berichten: 133
- Geregistreerd: 10 Jan 2016, 16:39
Re: 3 integers over serial
Wat heb je allemaal geprobeerd?
- nicoverduin
- Berichten: 5043
- Geregistreerd: 13 Mei 2013, 20:57
- Woonplaats: Heemskerk
Re: 3 integers over serial
bijvoorbeeld....
en... "Wat lukt niet?"
enz.
en... "Wat lukt niet?"
enz.
- Nightmystic
- Berichten: 133
- Geregistreerd: 10 Jan 2016, 16:39
Re: 3 integers over serial
Hahaha shit, de Dupont kabels zaten niet goed erin TX1 zat in RX2
Nu gebeurd er in ieder geval iets.
De getallen komen niet door zoals het zou moeten, maar er komt in ieder geval iets door.
Nu gebeurd er in ieder geval iets.
De getallen komen niet door zoals het zou moeten, maar er komt in ieder geval iets door.
Laatst gewijzigd door Nightmystic op 28 Apr 2016, 01:10, in totaal 1 keer gewijzigd.
- Nightmystic
- Berichten: 133
- Geregistreerd: 10 Jan 2016, 16:39
Re: 3 integers over serial
Zendende Arduino Mega:
cpp code
Ontvangende Arduino Mega
cpp code
De Seriele output van de ontvangende mega:
R 1681 | G 1012 | B 0
R 1691 | G 13 | B 0
R 1801 | G 80 | B 0
R 1811 | G 70 | B 0
R 1821 | G 60 | B 0
R 1831 | G 50 | B 0
R 1841 | G 40 | B 0
R 1851 | G 30 | B 0
R 1861 | G 20 | B 0
R 1871 | G 1010 | B 0
R 1881 | G 11 | B 0
R 189 | G 9012 | B 0
R 190 | G 8013 | B 0
R 191 | G 7014 | B 0
R 192 | G 6015 | B 0
R 193 | G 5016 | B 0
R 194 | G 4017 | B 0
R 195 | G 3018 | B 0
R 196 | G 2019 | B 0
R 1972 | G 20 | B 0
R 1981 | G 90 | B 0
R 1991 | G 80 | B 0
R 2001 | G 70 | B 0
R 2011 | G 60 | B 0
R 2021 | G 50 | B 0
R 2031 | G 40 | B 0
R 2041 | G 30 | B 0
R 2051 | G 20 | B 0
R 2061 | G 10 | B 0
R 2071 | G 0 | B 0
R 208 | G 9010 | B 0
R 209 | G 8011 | B 0
R 210 | G 7012 | B 0
R 211 | G 6013 | B 0
R 212 | G 5014 | B 0
R 213 | G 4015 | B 0
R 214 | G 3016 | B 0
cpp code
int R;
int G;
int B;
char buffer[10];
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}
void loop()
{
R = R + 1;
G = G - 10;
B = B + 10;
if (R > 254)
{
R = 0;
}
if (G < 20)
{
G = 200;
}
if (B > 200)
{
B = 0;
}
// Serial.print(R);Serial.print(" | "); Serial.print(G);Serial.print(" | ");Serial.println(B);
sprintf(buffer,"%03d%03d%03d",R,G,B);
Serial1.write(buffer);
delay(100);
}
Ontvangende Arduino Mega
cpp code
String readString, R, G, B;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}
void loop()
{
while (Serial1.available())
{
delay(10);
if (Serial1.available() >0)
{
char c = Serial1.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0)
{
// Serial.println(readString); //see what was received
R = readString.substring(0, 4); //get the first four characters
G = readString.substring(4, 8); //get the next four characters
B = readString.substring(8, 12); //get the last four characters
int n1; //declare as number
int n2;
int n3;
char carray1[6]; //magic needed to convert string to a number
R.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
G.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2);
char carray3[6];
B.toCharArray(carray3, sizeof(carray3));
n3 = atoi(carray3);
Serial.print("R "); Serial.print(n1); Serial.print(" | ");
Serial.print("G "); Serial.print(n2); Serial.print(" | ");
Serial.print("B "); Serial.println(n3);
readString="";
}
}
De Seriele output van de ontvangende mega:
R 1681 | G 1012 | B 0
R 1691 | G 13 | B 0
R 1801 | G 80 | B 0
R 1811 | G 70 | B 0
R 1821 | G 60 | B 0
R 1831 | G 50 | B 0
R 1841 | G 40 | B 0
R 1851 | G 30 | B 0
R 1861 | G 20 | B 0
R 1871 | G 1010 | B 0
R 1881 | G 11 | B 0
R 189 | G 9012 | B 0
R 190 | G 8013 | B 0
R 191 | G 7014 | B 0
R 192 | G 6015 | B 0
R 193 | G 5016 | B 0
R 194 | G 4017 | B 0
R 195 | G 3018 | B 0
R 196 | G 2019 | B 0
R 1972 | G 20 | B 0
R 1981 | G 90 | B 0
R 1991 | G 80 | B 0
R 2001 | G 70 | B 0
R 2011 | G 60 | B 0
R 2021 | G 50 | B 0
R 2031 | G 40 | B 0
R 2041 | G 30 | B 0
R 2051 | G 20 | B 0
R 2061 | G 10 | B 0
R 2071 | G 0 | B 0
R 208 | G 9010 | B 0
R 209 | G 8011 | B 0
R 210 | G 7012 | B 0
R 211 | G 6013 | B 0
R 212 | G 5014 | B 0
R 213 | G 4015 | B 0
R 214 | G 3016 | B 0
- Nightmystic
- Berichten: 133
- Geregistreerd: 10 Jan 2016, 16:39
Re: 3 integers over serial
Solved!
Changed:
R = readString.substring(0, 3); //get the first four characters
G = readString.substring(3, 6); //get the next four characters
B = readString.substring(6, 9); //get the last four characters
Output:
R 64 | G 170 | B 10
R 65 | G 160 | B 20
R 125 | G 130 | B 200
R 126 | G 120 | B 0
R 127 | G 110 | B 10
R 128 | G 100 | B 20
R 129 | G 90 | B 30
R 130 | G 80 | B 40
R 131 | G 70 | B 50
R 132 | G 60 | B 60
R 133 | G 50 | B 70
R 134 | G 40 | B 80
R 135 | G 30 | B 90
R 136 | G 20 | B 100
R 137 | G 200 | B 110
R 138 | G 190 | B 120
R 139 | G 180 | B 130
R 140 | G 170 | B 140
R 141 | G 160 | B 150
R 142 | G 150 | B 160
R 143 | G 140 | B 170
R 144 | G 130 | B 180
Changed:
R = readString.substring(0, 3); //get the first four characters
G = readString.substring(3, 6); //get the next four characters
B = readString.substring(6, 9); //get the last four characters
Output:
R 64 | G 170 | B 10
R 65 | G 160 | B 20
R 125 | G 130 | B 200
R 126 | G 120 | B 0
R 127 | G 110 | B 10
R 128 | G 100 | B 20
R 129 | G 90 | B 30
R 130 | G 80 | B 40
R 131 | G 70 | B 50
R 132 | G 60 | B 60
R 133 | G 50 | B 70
R 134 | G 40 | B 80
R 135 | G 30 | B 90
R 136 | G 20 | B 100
R 137 | G 200 | B 110
R 138 | G 190 | B 120
R 139 | G 180 | B 130
R 140 | G 170 | B 140
R 141 | G 160 | B 150
R 142 | G 150 | B 160
R 143 | G 140 | B 170
R 144 | G 130 | B 180
- Nightmystic
- Berichten: 133
- Geregistreerd: 10 Jan 2016, 16:39
Re: 3 integers over serial
Wat ik er uiteindelijk mee gedaan heb:
https://youtu.be/_AxHhikuA9U
https://youtu.be/_AxHhikuA9U
11 berichten
• Pagina 1 van 2 • 1, 2
Wie is er online?
Gebruikers in dit forum: Geen geregistreerde gebruikers en 6 gasten