Hello everyone, thank you for you time I already done my coding on master and slave. But on slave the arduino seem cannot produce output to light my LED (vertical and horizontal).
Here my coding for slave:
#include <Wire.h>
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
int ledPins2[] = {2, 3, 4, 5, 6, 7, 8, 9};
byte count;
byte nChar;
char c;
char input[8];
char data[50] = "";
int n;
#define nBits sizeof(ledPins)/sizeof(ledPins[0])
#define nBits sizeof(ledPins2)/sizeof(ledPins[0])
void setup()
{
Wire.begin(1);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
for (byte i = 0; i < nBits; i++) {
pinMode(ledPins[i], OUTPUT);
}
Serial.begin(9600);
}
void loop()
{
if (nChar < 7) { //accumulate three characters
if (Serial.available()) {
c = Serial.read();
input[nChar++] = c;
}
}
else {
input[7] = 0; //atoi() expects string terminator
n = atoi(input); //convert the input characters to integer
dispBinary(n);
delay(100); //wait for any additional characters
while (Serial.available()) { //ignore them
c = Serial.read();
}
nChar = 0;
}
}
void receiveEvent(int howMany)
{
while (1 < Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
}
void dispBinary(int n) //Vertical
{
if (n >= 0 && n <= 5) {
for (byte i = 0; i < nBits; i++) {
digitalWrite (ledPins[i], n & 1);
n /= 2;
}
}
}
void dispBinary2(int n) //Horizontal
{
if (n >= 0 && n <= 5) {
for (byte i = 0; i < nBits; i++) {
digitalWrite (ledPins2[i], n & 1);
n /= 2;
}
}
}
void b()
{
if (data[0] != 'a')
{
dispBinary(atoi("00"));
delay(100);
dispBinary2(atoi("00"));
delay(100);
goto fin;
}
if (data[1] = 'x')
{
char xdata[2] = {data[2], data[3]};//(data[2], data[3]);
dispBinary2(atoi(xdata));
delay(100);
}
if (data[4] = 'y')
{
char ydata[2] = {data[5], data[6]};//(data[5], data[6]);
dispBinary(atoi(ydata));
delay(100);
}
fin:;
}
Sorry I use 74154 to leds matrix y-axis and 74138 to leds matrix x-axis and 7404 inversor. I connect the leds with IDE cable which 5-horizontal and 5-vertical.
Also asked at: https://arduino.stackexchange.com/q/56547 If you're going to do that then at least be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information.
Quick question. Why does your software use pins 1, 2, 3, 4, 5, 6, 7, 8 and 9 but your diagram uses pins 6, 7, 8, 9, 10, 11 and 12?
Your displayBinary function:-
void dispBinary(int n) //Vertical
{
if (n >= 0 && n <= 5) {
for (byte i = 0; i < nBits; i++) {
digitalWrite (ledPins[i], n & 1);
n /= 2;
}
}
}
will only ever output HIGHs to pins 1, 2 and 3 which are not used in your diagram. Pins 4-9 will be permanently low and 10, 11 and 12 will be pulled low by your 1k pull-down resistors as they are never set to be outputs.
The same is true for the displayBinary2 function which is only called by the b function which itself is never called
You also have an issue with this line in loop:-
input[8] = 0; //atoi() expects string terminator
input is defined in this line:-
char input[8];
which means it is an array of 8 char with indexes 0-7. input[8] is beyond the bounds of the array. This means you are writing 0 into a byte of some other variable. The problem is you don’t know what that going to affect.