Hello everyone.
This is one of my first project with programming, i'm problably missing a few basics i would need.
The Hardware looks like this:
Sender:
-Ardunio UNO with EthernetShield and ElectricImp ---> Receive OSC-Message and send a simple String "r", "g" or "b" (only once) over impSerial.write() whenever the color has changed, send the value rot, gruen or blau
Receiver:
-Arduino Uno with ElectricImp, recieve byte and set the color of the rgb strip to all pixels
How do i convert three ascii values (dez) for example 49, 55, 55 to a int that i can use in the rest of the code.
I already read through http://www.gammon.com.au/forum/?id=11425 (that's where my code ist from)
I've tried with atoi, Serial.parseInt() and so on, but it never worked.
void processIncomingByte (const byte c)
{
if (isdigit (c))
{
.....
.....
Code of the Sender:
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Ethernet.h>
#include <ArdOSC.h>
SoftwareSerial impSerial(8, 9); // RX on 8, TX on 9
byte myMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte myIp[] = { 10, 0, 0, 88 };
int serverPort = 8000;
int destPort=12000;
int lastState = 0;
int currentState = 0;
int rot = 0;
int gruen = 0;
int blau = 0;
OSCServer server;
OSCClient client;
String stringR, stringG, stringB;
void setup(){
stringR = String("r");
stringG = String("g");
stringB = String("b");
impSerial.begin(19200);
Serial.begin(19200);
Ethernet.begin(myMac ,myIp);
server.begin(serverPort);
server.addCallback("/knbr",&OSCrot);
server.addCallback("/knbg",&OSCgruen);
server.addCallback("/knbb",&OSCblau);
}
void loop(){
if(server.aviableCheck()>0){}
}
void OSCrot(OSCMessage *_mes){
rot = int( _mes->getArgFloat(0)*255.f);
currentState= 1;
if (lastState != currentState) // send the string r whenever color changed
{
impSerial.print(stringR);
Serial.println("StateChanged"); // just for debugging
}
impSerial.print(rot); // send the value of red
lastState = currentState; // store the current state
} // end of void OSCrot
void OSCgruen(OSCMessage *_mes){
gruen= int( _mes->getArgFloat(0)*255.f);
currentState= 2;
if (lastState != currentState) // send the string g whenever color changed
{
impSerial.print(stringG);
Serial.println("StateChanged"); // just for debugging
}
impSerial.print(gruen); // send the value of green
lastState = currentState; // store the current state
} // end of void OSCgruen
void OSCblau(OSCMessage *_mes){
blau= int( _mes->getArgFloat(0)*255.f);
currentState= 3;
if (lastState != currentState) // send the string b whenever color changed
{
impSerial.print(stringB);
Serial.println("StateChanged"); // just for debugging
}
impSerial.print(blau); // send the value of blue
lastState = currentState; // store the current state
} // end of void OSCblau
Code of the Receiver:
// Most of the Code ist from http://www.gammon.com.au/forum/?id=11425
#include <SoftwareSerial.h>
#include "SPI.h"
#include "WS2801.h"
#include <stdlib.h>
SoftwareSerial impSerial(8, 9); // RX on 8, TX on 9
// the possible states of the state-machine
typedef enum { NONE, GOT_R, GOT_G, GOT_B } states;
// current state-machine state
states state = NONE;
// current partial number
int receivedNumber = 0;
int r = 0;
int g = 0;
int b = 0;
int dataPin = 2; // for the WS2802 strip
int clockPin = 3; // for the WS2802 strip
WS2801 strip = WS2801(25, dataPin, clockPin);
boolean gotR = LOW;
boolean gotG = LOW;
boolean gotB = LOW;
String stringR, stringG, stringB;
void setup ()
{
stringR = String('r');
stringG = String('g');
stringB = String('b');
strip.begin();
impSerial.begin(19200); // Serial for receiving from the electric Imp
Serial.begin (19200); // Serial for debugging
} // end of setup
void processIncomingByte (const byte c)
{
if (isdigit (c))
{
//**************************The part i cant figure out************************
// Serial.println(receivedNumber);
switch (state)
{
case GOT_R:
r = receivedNumber;
Serial.println(r);
break;
case GOT_G:
g = receivedNumber;
Serial.println(g);
break;
case GOT_B:
b = receivedNumber;
Serial.println(b);
break;
} // end of switch state
} // end of digit
else
{
// set the new state, if we recognize it
switch (c)
{
case 'r':
state = GOT_R;
Serial.println(" Received an r ");
break;
case 'g':
state = GOT_G;
Serial.println(" Received an g ");
break;
case 'b':
state = GOT_B;
Serial.println(" Received an b ");
break;
default:
state = NONE;
break;
} // end of switch on incoming byte
} // end of not digit
} // end of processIncomingByte
void loop ()
{
if (impSerial.available ())
processIncomingByte (impSerial.read ());
for ( int i = 0; i <= 10; i++)
{
strip.setPixelColor(i, r,g,b); // set Color to all Pixels
}
strip.show();
} // end of loop