PaulS:
the reason I would like to understand the code is because my arduino not UNO but I am using MEGA...
Then you have a relatively major job ahead of you, and you will most definitely need to understand the code. The reason for this is that Firmata currently does not play well with the Mega (which has lot more pins, including more PWM pins, more analog pins) and more timers supporting more servos - none of which Firmata knows about.
No one from the Firmata community seems interested in improving Firmata to work with the Mega, so thank you for volunteering.
I feel discourage..Because I do not have any basic on VB and that Arduino Firmata Sketch.
Do you have any recommendation where I should start?
Why no need to understand the sketch?
As far as your original intent, modifying a GUI on a PC, there is no need to understand exactly how the Arduino responds to a Firmata-formatted request to read a digital pin or set a PWM value or read an analog pin. All you need to know is that it does.
Actually, I have my sketch code which has worked well without using VB, just directly from Arduino UNO to 7segment display and sensors.
...
// Variable holding the timer value so far. One for each "Timer"
unsigned long redLED0timer;
unsigned long redLED1timer;
unsigned long redLED2timer;
*/
// Definitions of the 7-bit values for displaying digits
byte g_digits [10];
//Machine Config in integer array
int MachineConfig[3][3];
// Current number being displayed
int g_numberToDisplay = 0;
// Number of shift registers in use
const int g_registers = 4;
// Array of numbers to pass to shift registers
byte g_registerArray [g_registers];
void setup()
{
//pinMode (redLED0,OUTPUT);
//pinMode (redLED1,OUTPUT);
//pinMode (redLED2,OUTPUT);
Serial.begin(9600);
pinMode (IRdetectPin0,INPUT);
pinMode (IRdetectPin1,INPUT);
pinMode (IRdetectPin2,INPUT);
pinMode (pinCommLatch1,OUTPUT);
pinMode (pinData1,OUTPUT);
pinMode (pinClock1,OUTPUT);
pinMode (switchPin0,INPUT);
pinMode (switchPin1,INPUT);
pinMode (switchPin2,INPUT);
//config for line 1
MachineConfig[0][0]=2; //pinCommLatch line 1
MachineConfig[0][1]=5; //pinData line 1
MachineConfig[0][2]=8; //pinClock line 1
//config for line 2
MachineConfig[1][0]=3; //pinCommLatch line 2
MachineConfig[1][1]=6; //pinData line 2
MachineConfig[1][2]=9; //pinClock line 2
//config for line 3
MachineConfig[2][0]=4; //pinCommLatch line 3
MachineConfig[2][1]=7; //pinClock line 3
MachineConfig[2][2]=10; //pinData line 3
// Setup the digits array
g_digits [0] = 1 + 2 + 4 + 8 + 16 + 32 + 00;
g_digits [1] = 0 + 2 + 4 + 0 + 00 + 00 + 00;
g_digits [2] = 1 + 2 + 0 + 8 + 16 + 00 + 64;
g_digits [3] = 1 + 2 + 4 + 8 + 00 + 00 + 64;
g_digits [4] = 0 + 2 + 4 + 0 + 00 + 32 + 64;
g_digits [5] = 1 + 0 + 4 + 8 + 00 + 32 + 64;
g_digits [6] = 1 + 0 + 4 + 8 + 16 + 32 + 64;
g_digits [7] = 1 + 2 + 4 + 0 + 00 + 00 + 00;
g_digits [8] = 1 + 2 + 4 + 8 + 16 + 32 + 64;
g_digits [9] = 1 + 2 + 4 + 8 + 00 + 32 + 64;
//for loop 0-2 int
for(int i=0;i<1;i++){
sendSerialDatainitial(g_registers, g_registerArray,i);
}
}
void sendSerialDatainitial(
byte registerCount, // How many shift registers?
byte *pValueArray,int fromLine) // Array of bytes with LSByte in array [0]
{
g_registerArray [3] = g_digits [0];
g_registerArray [2] = g_digits [0];
g_registerArray [1] = g_digits [0];
g_registerArray [0] = g_digits [0];
int PINCommLatch=MachineConfig[fromLine][0];
int PINData=MachineConfig[fromLine][1];
int PINClock=MachineConfig[fromLine][2];
pinMode (PINCommLatch, OUTPUT);
pinMode (PINData, OUTPUT);
pinMode (PINClock, OUTPUT);
// Signal to the 595s to listen for data
digitalWrite (PINCommLatch, LOW);
for (byte reg = registerCount; reg > 0; reg--)
{
byte value = pValueArray [reg-1];
for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
{
digitalWrite (PINClock, LOW);
digitalWrite (PINData, value & bitMask ? HIGH : LOW);
digitalWrite (PINClock, HIGH);
}
}
// Signal to the 595s that I'm done sending
digitalWrite (PINCommLatch, HIGH);
} // sendSerialData
// Simple function to send serial data to one or more shift registers by iterating backwards through an array.
// Although g_registers exists, they may not all be being used, hence the input parameter.
void sendSerialData (
byte registerCount, // How many shift registers?
byte *pValueArray, // Array of bytes with LSByte in array [0]
int fromLine)
{
int PINCommLatch=MachineConfig[fromLine][0];
int PINData=MachineConfig[fromLine][1];
int PINClock=MachineConfig[fromLine][2];
// Signal to the 595s to listen for data
digitalWrite (PINCommLatch, LOW);
for (byte reg = registerCount; reg > 0; reg--)
{
byte value = pValueArray [reg - 1];
for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
{
digitalWrite (PINClock, LOW);
digitalWrite (PINData, value & bitMask ? HIGH : LOW);
digitalWrite (PINClock, HIGH);
}
}
// Signal to the 595s that I'm done sending
digitalWrite (PINCommLatch, HIGH);
} // sendSerialData
void loop()
{
if (digitalRead(switchPin0))// START BUTTON LINE 1
{
detect(0);
}
if (digitalRead(switchPin1))// START BUTTON LINE 2
{
detect(1);
}
if (digitalRead(switchPin2))// START BUTTON LINE 3
{
detect(2);
}
}
void detect(int line)
{
if (line == 0)
{
int state0 = digitalRead(IRdetectPin0);
if (laststate0 == HIGH && state0 == LOW) // only count on a LOW-> HIGH transition
{
increment (0);
Serial.println(no1);
}
laststate0 = state0;// remember last state
}
if (line == 1)
{
int state1 = digitalRead(IRdetectPin1);
if (laststate1 == HIGH && state1 == LOW) // only count on a LOW-> HIGH transition
{
increment (1);
Serial.println(no2);
}
laststate1 = state1; // remember last state
}
if (line == 2)
{
int state2 = digitalRead(IRdetectPin2);
if (laststate2 == LOW && state2 == HIGH) // only count on a LOW-> HIGH transition
{
increment (2);
Serial.println(no2);
}
laststate2 = state2; // remember last state
}
}
/*void blinks()
{
// Handling the blink of one LED.
if ( (millis () - redLED0timer) >= redLED0interval)
{
digitalWrite (redLED0, HIGH);
digitalWrite (redLED0, LOW);
increment (0);
redLED0timer = millis ();
}
if ( (millis () - redLED1timer) >= redLED1interval)
{
digitalWrite (redLED1, HIGH);
digitalWrite (redLED1, LOW);
increment (1);
redLED1timer = millis ();
}
if ( (millis () - redLED2timer) >= redLED2interval)
{
digitalWrite (redLED2, HIGH);
digitalWrite (redLED2, LOW);
increment (2);
redLED2timer = millis ();
}
}*/
/* int line1 = random (1,3);
int line2 = random (1,4);
int line3 = random (1,6);
int k=0;
if(line1==1)
{
digitalWrite(2, HIGH); // set the LED on
delay(300);
digitalWrite(2, LOW);
increment(0);
}
if(line2==1)
{
digitalWrite(3, HIGH); // set the LED on
delay(300);
digitalWrite(3, LOW);
increment(1);
}
if(line3==1)
{
digitalWrite(4, HIGH); // set the LED on
delay (200);
digitalWrite(4, LOW);
increment(2);
}
}*/
void increment(int line)
{
if(line==0)
{
g_numberToDisplay = no1;
no1++;
show(g_numberToDisplay,line);
}else if(line==1)
{
g_numberToDisplay = no2;
no2++;
show(g_numberToDisplay,line);
}
else if(line==2)
{
g_numberToDisplay = no3;
no3++;
show(g_numberToDisplay,line);
}
}
void show(int numberToDisplay,int fromLine)
{
if (numberToDisplay < 10)
{
g_registerArray [3] = g_digits [0];
g_registerArray [2] = g_digits [0];
g_registerArray [1] = g_digits [0];
g_registerArray [0] = g_digits [numberToDisplay];
}
else if (numberToDisplay < 100)
{
...
}
Then If I do not understand how Arduino responds to a Firmata-formatted request to read a digital pin or set a PWM value or read an analog pin, how I could know at which part I must insert my code?
I am sorry for this very long and unconvenience question that might disturb you.