I got a problem with my slave coding

Can someone help me with the coding it (slave code)

#include <Wire.h>

void setup()
{
Wire.begin(1);
Wire.onReceive(receiveEvent);
Serial.begin(9600);

}

void loop() {
}

void receiveEvent(int howMany)
{
while(7 < Wire.available()){
char c = Wire.read();
Serial.print(c);
}
{
int x = Wire.read();
Serial.println(x);
}
{
int y = Wire.read();
Serial.println(y);
}

}
void dispBinary(int n) //Vertical
{
if (n >= 0 && n <= 5){
for (byte i=0; i<nBits; i++) {digitalWrite (ledPins*, n & 1); n/=2;}*

  • }*
  • }*
    void dispBinary2(int n) //Horizontal
    {
  • if (n >= 0 && n <= 5){*
    _ for (byte i=0; i<nBits; i++) {digitalWrite (ledPins2*, n & 1); n/=2;}_
    _
    }*_

}
void b()
{
* dispBinary(atoi(“00”));*
* delay(100);*
* dispBinary2(atoi(“00”));*
* delay(100);*
* goto fin;*
}
if (data[1] = ‘x’)
{
* char xdata[2] = (data[2],data[3]);*
* dispBinary2(atoi(xdata));*
* delay(100);*
}
if (data[4] = ‘y’)
{
* char ydata[2] = (data[5],data[6]);*
* dispBinary(atoi(ydata));*
* delay(100);*
}
fin:;
}

Dude. I don't think you can say that!

Why does your code get itallicy in the middle. There is a "How to use this forum" topic that might help you find the answer in a better way.

He probably used [ i ] somewhere in the code :grinning: . First of all, post your code in code tags - it becomes easier to read. And please explain to as: 1. What do you want this code to do. 2. What is actually does. 3. Post errors or solutions you tried.

This way you're saving our and your time! :) Thanks.

I want to make from binary to LED output for (serial and parallel) like a matrix

Here my master coding

// Master Code
#include <Wire.h>
byte nChar =0;
char input[7];

void setup () {

Serial.begin(9600);
Wire.begin();
}

void loop()
{
if (nChar < 7) {if (Serial.available()) { input[nChar++] = Serial.read();}}

else {

Wire.beginTransmission(1);

//a
Wire.write(input[0]);
Serial.print("Armoire : ");
Serial.println(input[0]);

//x
Wire.write(input[1]);
Serial.print("Output : ");
Serial.println(input[1]);

//00
Wire.write(input[2]);
Wire.write(input[3]);
Serial.print("Output : ");
Serial.print(input[2]);
Serial.println(input[3]);

//y
Wire.write(input[4]);
Serial.print("Output : ");
Serial.println(input[4]);

//00
Wire.write(input[5]);
Wire.write(input[6]);
Serial.print("Output : ");
Serial.print(input[5]);
Serial.println(input[6]);

delay(15);

while (Serial.available())
{
//Remove extra
Serial.read();
}
Wire.endTransmission();
nChar = 0;
}
}

Here my slave code

#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];
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*, 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*, n & 1); n/=2;}_
    _
    }_
    _
    }_
    void dispBinary2(int n) //Horizontal
    _
    {_
    _
    if (n >= 0 && n <= 5){_
    _ for (byte i=0; i<nBits; i++) {digitalWrite (ledPins2, 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]);*
* dispBinary2(atoi(xdata));*
* delay(100);*
}
if (data[4] = ‘y’)
{
* char ydata[2] = (data[5],data[6]);*
* dispBinary(atoi(ydata));*
* delay(100);*
}
fin:;
}

My error (on slave coding)
Arduino: 1.8.5 (Windows 10), Board: “Arduino Nano, ATmega328P”
C:\Users\nafis\Desktop\slave_coding_fyp\slave_coding_fyp.ino:10:0: warning: “nBits” redefined
#define nBits sizeof(ledPins2)/sizeof(ledPins[0])
^
C:\Users\nafis\Desktop\slave_coding_fyp\slave_coding_fyp.ino:9:0: note: this is the location of the previous definition
#define nBits sizeof(ledPins)/sizeof(ledPins[0])
^
C:\Users\nafis\Desktop\slave_coding_fyp\slave_coding_fyp.ino: In function ‘void b()’:
slave_coding_fyp:71: error: ‘data’ was not declared in this scope
* if (data[0] != ‘a’)*
* ^*
slave_coding_fyp:79: error: ‘data’ was not declared in this scope
if (data[1] = ‘x’)
* ^*
slave_coding_fyp:85: error: ‘data’ was not declared in this scope
if (data[4] = ‘y’)
* ^*
exit status 1
‘data’ was not declared in this scope
This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

There's a handy-dandy link at the top of every forum page titled: Read this before posting a programming question ...

Despite this rather obvious title, you appear to have either not read the post or ignored the advice it contains. Please (re)read it now. Pay particular attention to Item #6 which tell you how to post your code using CODE TAGS so that it doesn't look so ugly and is easier for folks to read. Also, type 'ctrl-t' in the Arduino IDE to format you code with proper indentations.

Doing these things will make your posts and code much more readable. Remember, you're asking people on the forum to help you for FREE. The easier you make it for them, the more inclined they will be to do so. On the other hand, ignoring the well-documented forum guidelines displays a disrespect that will discourage people from helping you.

@Op

These are your slave codes without any compilation errors. I have not done any change in your codes, but I have corrected two syntax errors and added an array (char data[50] = “”;). (The original codes are kept in the same line under comment.)

#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:;
}

Thank you very much. Next time I will follow the rule and regulation. Sir Golam you really helpful. May God bless you. Assalamualaikum