I'm trying to create different nodes that write to PCF8574 chips, which are 8-channel IO expanders that the arduino talks to via I2C.
The following
<EmbrioCopy>
<Version>1.3.5.0</Version>
<Nodes>
<Node>
<UniqueID>02767b72-a46c-4886-a45e-e4fe1207bb69</UniqueID>
<AssemblyType>Embrio.NodeEngine.AgentController.Nodes.AgentControllerCustomArduinoNode</AssemblyType>
<NodeType>Custom Arduino Node</NodeType>
<UserDefinedName>PCF8574</UserDefinedName>
<Description>A node that lets you write any Arduino code to interact with the Arduino hardware. Can be both an input and output.</Description>
<NodeColor>144,183,227,255</NodeColor>
<X>0</X>
<Y>0</Y>
<Width>364</Width>
<IncludeCode>#include <Wire.h></IncludeCode>
<DeclerationCode></DeclerationCode>
<SetupCode>#ifndef WIRE_BEGIN
#define WIRE_BEGIN
Wire.begin();
#endif // WIRE_BEGIN</SetupCode>
<ImageName>arduino.png</ImageName>
<InfoURL></InfoURL>
<IsOpen>True</IsOpen>
<EditWidth>364</EditWidth>
<CodeDefinitions>
<CodeDefinition>
<UpdateCondition>EveryUpdate</UpdateCondition>
<Code>byte byteToSend =
(Input_Activation0 &gt; 0.5) &lt;&lt; 0 |
(Input_Activation1 &gt; 0.5) &lt;&lt; 1 |
(Input_Activation2 &gt; 0.5) &lt;&lt; 2 |
(Input_Activation3 &gt; 0.5) &lt;&lt; 3 |
(Input_Activation4 &gt; 0.5) &lt;&lt; 4 |
(Input_Activation5 &gt; 0.5) &lt;&lt; 5 |
(Input_Activation6 &gt; 0.5) &lt;&lt; 6 |
(Input_Activation7 &gt; 0.5) &lt;&lt; 7 |
0x0;
Wire.beginTransmission(0x20+(byte)Input_I2CAddress);
Wire.write(byteToSend);
Wire.endTransmission();</Code>
<UniqueID>98bc326b-1337-4911-8a4f-70dc4a5be836</UniqueID>
</CodeDefinition>
</CodeDefinitions>
<Inputs>
<Input>
<UniqueID>3d831054-1036-4937-8bbb-c6ffc431a6e2</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation</DefaultName>
<CustomName>Activation 0</CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>ec6b8a02-cb63-445c-813b-29174895a11d</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 1</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0.852</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>0c188919-e70a-4d08-8c6b-3cf5b84d1a8d</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 2</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0.868</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>2e7388fb-a81f-45c5-91bf-68ea1558f5ae</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 3</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0.857</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>b0e2f3ca-ce62-4716-a680-b2d6ac724dfe</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 4</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>dac8ccd1-802f-4724-8be9-3836ebd9710a</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 5</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0.841</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>959e08be-18d0-47f3-930b-76f7ccc71a3c</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 6</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>cfad579c-f8b4-4e8e-a4db-52bfaefd6997</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 7</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0.863</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>17497bdb-a5d0-40f3-82fa-3f8517503d79</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Integer</DefaultName>
<CustomName>I2C Address</CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Integer</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.IntegerNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<DisconnectedValue>0</DisconnectedValue>
<MinValue>0</MinValue>
<MaxValue>7</MaxValue>
</Input>
</Inputs>
<Outputs></Outputs>
</Node>
<Node>
<UniqueID>6e1ed0a0-314e-4ecf-82be-65475b042605</UniqueID>
<AssemblyType>Embrio.NodeEngine.AgentController.Nodes.AgentControllerCustomArduinoNode</AssemblyType>
<NodeType>Custom Arduino Node</NodeType>
<UserDefinedName>PCF8574</UserDefinedName>
<Description>A node that lets you write any Arduino code to interact with the Arduino hardware. Can be both an input and output.</Description>
<NodeColor>144,183,227,255</NodeColor>
<X>451.180769230769</X>
<Y>19.0954545454546</Y>
<Width>364</Width>
<IncludeCode>#include <Wire.h></IncludeCode>
<DeclerationCode></DeclerationCode>
<SetupCode>#ifndef WIRE_BEGIN
#define WIRE_BEGIN
Wire.begin();
#endif // WIRE_BEGIN</SetupCode>
<ImageName>arduino.png</ImageName>
<InfoURL></InfoURL>
<IsOpen>True</IsOpen>
<EditWidth>364</EditWidth>
<CodeDefinitions>
<CodeDefinition>
<UpdateCondition>EveryUpdate</UpdateCondition>
<Code>byte byteToSend =
(Input_Activation0 &gt; 0.5) &lt;&lt; 0 |
(Input_Activation1 &gt; 0.5) &lt;&lt; 1 |
(Input_Activation2 &gt; 0.5) &lt;&lt; 2 |
(Input_Activation3 &gt; 0.5) &lt;&lt; 3 |
(Input_Activation4 &gt; 0.5) &lt;&lt; 4 |
(Input_Activation5 &gt; 0.5) &lt;&lt; 5 |
(Input_Activation6 &gt; 0.5) &lt;&lt; 6 |
(Input_Activation7 &gt; 0.5) &lt;&lt; 7 |
0x0;
Wire.beginTransmission(0x20+(byte)Input_I2CAddress);
Wire.write(byteToSend);
Wire.endTransmission();</Code>
<UniqueID>98bc326b-1337-4911-8a4f-70dc4a5be836</UniqueID>
</CodeDefinition>
</CodeDefinitions>
<Inputs>
<Input>
<UniqueID>a55767e1-6796-428b-96c7-7755c94bc05a</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation</DefaultName>
<CustomName>Activation 0</CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>74a43e17-a9fc-473e-8b96-936ca957b711</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 1</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>dd303f58-a6ac-4e42-a9d9-2c2529993b32</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 2</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0.868</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>733927ab-968e-4fa8-a918-3798d718f984</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 3</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0.857</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>fa5f12c6-f4d7-46d2-9f2a-9b71032f5028</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 4</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>1</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>3e9481c7-8fa5-4ccd-9562-9a5387c9e829</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 5</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>46423d86-32b4-4f55-83eb-89bc1e9e3716</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 6</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>a5bc8d53-bc24-4fcc-845e-33b7137f306e</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Activation 7</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<BlendMode>Add</BlendMode>
<DisconnectedValue>0.863</DisconnectedValue>
<UseNegativeRange>False</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Input>
<Input>
<UniqueID>32152b9a-8cb9-4efe-878e-5d8931ec2190</UniqueID>
<DefinitionID>00000000-0000-0000-0000-000000000000</DefinitionID>
<DefaultName>Integer</DefaultName>
<CustomName>I2C Address</CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Integer</TypeName>
<IsOutsideConnection>True</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.IntegerNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<IsUserAdded>True</IsUserAdded>
<ConnectedOutputs></ConnectedOutputs>
<DisconnectedValue>1</DisconnectedValue>
<MinValue>0</MinValue>
<MaxValue>7</MaxValue>
</Input>
</Inputs>
<Outputs></Outputs>
</Node>
</Nodes>
</EmbrioCopy>
contains two identical custom arduino nodes. Each is responsible for communicating with one PCF8574, which have I2C addresses from 0x20 to 0x27 -- hence I give the last hex digit as an integer input to the custom node. I have two PCF8574 chips on a breadboard, each controlling eight LEDs. If I delete one node, the remaining node can control either of the two chips, depending on which I select, from Embrio's "connected" mode. Adjusting an activation input will immediately light or darken an LED on the breadboard. With both nodes, however, neither node can communicate correctly with the I2C lines -- random lights light up on both chips, and changes seem to happen several times after I change the activation values, if ever.
The two above nodes, when together, produce the following code:
#include <Arduino.h>
#include "EmbrioMath.h"
#include "EmbrioArduino.h"
#include "Wire.h"
boolean handleSerial = false;
int refreshRateToUpdate = 0;
int sendHashCount = 0;
boolean rate5changed = false;
// Declare the arrays that store all data
float f[16];
bool b[16];
long l[2];
#line 0 "02767b72-a46c-4886-a45e-e4fe1207bb69_Declaration"
#line 0 "6e1ed0a0-314e-4ecf-82be-65475b042605_Declaration"
float ReadFloat()
{
byte bytes[4];
Serial.readBytes(bytes, 4);
return *(float *)&bytes;
}
long ReadLong()
{
byte bytes[4];
Serial.readBytes(bytes, 4);
return *(long *)&bytes;
}
String ReadString(String currentValue)
{
String newString = "";
int characterCount = 0;
if (Serial.available() > 0)
characterCount = Serial.read();
char newChar;
int index = 0;
bool continueRead = true;
while (continueRead)
{
if (Serial.available() > 0)
{
newChar = Serial.read();
newString += newChar;
index++;
if (index >= characterCount)
continueRead = false;
}
else
{
continueRead = false;
}
}
return newString;
}
// Returns true after UPD is received on the serial port
boolean StartRead()
{
long stopLookingMicros = micros() + 1000; // only look for 1 millisecond
boolean gotU = false;
boolean gotP = false;
boolean gotD = false;
while (true)
{
if (Serial.available() > 0)
{
byte value = Serial.read();
if (!gotU && value == 'U') gotU = true;
else if (!gotP && value == 'P') gotP = true;
else if (!gotD == 'D') gotD = true;
else
{
refreshRateToUpdate = value;
return true;
}
}
// Cancel if we are left waiting too long
if (micros() > stopLookingMicros)
return false;
}
return false;
}
// Reads the next input value update and copy the values to the buffer.
void HandleIncomingCommand()
{
if (StartRead())
{
if (refreshRateToUpdate == 5)
{
f[0]=ReadFloat();
f[1]=ReadFloat();
f[2]=ReadFloat();
f[3]=ReadFloat();
f[4]=ReadFloat();
f[5]=ReadFloat();
f[6]=ReadFloat();
f[7]=ReadFloat();
l[0]=ReadLong();
f[8]=ReadFloat();
f[9]=ReadFloat();
f[10]=ReadFloat();
f[11]=ReadFloat();
f[12]=ReadFloat();
f[13]=ReadFloat();
f[14]=ReadFloat();
f[15]=ReadFloat();
l[1]=ReadLong();
rate5changed = true;
}
}
}
// Copy data from incoming update commands until the buffer is empty or a time limit expires.
// This prevents problems when the code sending updates is sending them faster than this code runs.
void HandleSerial()
{
rate5changed = false;
handleSerial = true;
long endTime = micros() + 2000; // Try for 2 milliseconds
while (handleSerial)
{
HandleIncomingCommand();
// If there is no more data to read we can stop
if (Serial.available() == 0)
handleSerial = false;
// Stop anyway after a timeout
else if (micros() > endTime)
handleSerial = false;
}
if (rate5changed)
{
b[0]=true;
b[1]=true;
b[2]=true;
b[3]=true;
b[4]=true;
b[5]=true;
b[6]=true;
b[7]=true;
b[16]=true;
b[8]=true;
b[9]=true;
b[10]=true;
b[11]=true;
b[12]=true;
b[13]=true;
b[14]=true;
b[15]=true;
b[17]=true;
}
}
void SendInputs5()
{
Serial.print("UPD:5");
Serial.print("\n");
}
void _Agent_PCF857402767()
{
#line 0 "02767b72-a46c-4886-a45e-e4fe1207bb69_EveryUpdate"
byte byteToSend =
(f[0] > 0.5) << 0 |
(f[1] > 0.5) << 1 |
(f[2] > 0.5) << 2 |
(f[3] > 0.5) << 3 |
(f[4] > 0.5) << 4 |
(f[5] > 0.5) << 5 |
(f[6] > 0.5) << 6 |
(f[7] > 0.5) << 7 |
0x0;
Wire.beginTransmission(0x20+(byte)l[0]);
Wire.write(byteToSend);
Wire.endTransmission();
}
void _Agent_PCF85746e1ed()
{
#line 0 "6e1ed0a0-314e-4ecf-82be-65475b042605_EveryUpdate"
byte byteToSend =
(f[8] > 0.5) << 0 |
(f[9] > 0.5) << 1 |
(f[10] > 0.5) << 2 |
(f[11] > 0.5) << 3 |
(f[12] > 0.5) << 4 |
(f[13] > 0.5) << 5 |
(f[14] > 0.5) << 6 |
(f[15] > 0.5) << 7 |
0x0;
Wire.beginTransmission(0x20+(byte)l[1]);
Wire.write(byteToSend);
Wire.endTransmission();
}
void SendHash()
{
if (sendHashCount++ > 32)
{
Serial.print("inf:BABAl\n");
sendHashCount = 0;
}
}
void Group_32_1()
{
ProcessTimedFunction(_Agent_PCF857402767, 6);
ProcessTimedFunction(_Agent_PCF85746e1ed, 6);
ProcessTimedFunction(HandleSerial, 1200);
ProcessTimedFunction(SendHash, 120);
ProcessTimedFunction(SendInputs5, 120);
}
void setup()
{
f[0]=0;
b[0]=true;
f[1]=0.056;
b[1]=true;
f[2]=0.056;
b[2]=true;
f[3]=0.056;
b[3]=true;
f[4]=0;
b[4]=true;
f[5]=0.078;
b[5]=true;
f[6]=0;
b[6]=true;
f[7]=0.095;
b[7]=true;
#line 0 "02767b72-a46c-4886-a45e-e4fe1207bb69_Startup"
#ifndef WIRE_BEGIN
#define WIRE_BEGIN
Wire.begin();
#endif
f[8]=0.862;
b[8]=true;
f[9]=0.834;
b[9]=true;
f[10]=0.868;
b[10]=true;
f[11]=0.857;
b[11]=true;
f[12]=1;
b[12]=true;
f[13]=0.878;
b[13]=true;
f[14]=0.884;
b[14]=true;
f[15]=0.863;
b[15]=true;
#line 0 "6e1ed0a0-314e-4ecf-82be-65475b042605_Startup"
#ifndef WIRE_BEGIN
#define WIRE_BEGIN
Wire.begin();
#endif
Serial.begin(57600);
}
void loop()
{
ProcessTimedFunction(Group_32_1, 1452);
EmbrioWait(29798);
}
It's possible for the arduino to control both chips without Emrbio. The following sketch:
#include "Wire.h"
void setup() {
// put your setup code here, to run once:
Wire.begin();
}
unsigned short i = 0;
void loop() {
// put your main code here, to run repeatedly:
Wire.beginTransmission(0x20);
Wire.write(i&0xFF);
Wire.endTransmission();
Wire.beginTransmission(0x21);
Wire.write(i>>8);
Wire.endTransmission();
i++;
delay(50);
}
causes both chips to work in tandem, counting from 0 to 65536, representing ones with lit LEDs and zeroes with unlit LEDs.
I'm using Embrio 1.3.5.0, Arduino IDE 1.6.6, and I'm running on an arduino R3. I'm using Windows 7 64 bit.
I can provide a Fritzing diagram or a schematic if needed; Fritzing is most appropriate for this but I never use Fritzing so I'd have to figure out how to do it. Essentially, the arduino powers the board, both chips get +5V, I use two 10K resistors as pullups for the I2C lines, and each chip is connected to both I2C lines and is directly connected to eight LEDs, no resistors on the LEDs (I don't even think I have 16 220 ohm resistors).