I got an assignment where I have to accurately send and recieve a number, but I have to send it over wire. Therefore I have to turn it into bytes, but the arduino uno can only read one byte at a time. I found a code which would solve my problem, but I am getting an error message and I don't know how to solve it.
The error code is: a function-definition is not allowed here before '{' token
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
// Written by Nick Gammon
// May 2012
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
#define DATABUFFERSIZE 6
char dataBuffer[DATABUFFERSIZE+1]; //Add 1 for NULL terminator
byte dataBufferIndex = 0;
}
void loop() {
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
char startChar = '%'; // or '!', or whatever your start character is
char endChar = '&';
boolean getSerialString(){
static byte dataBufferIndex = 0;
while(Wire.available()>0){
char incomingbyte = Wire.read();
if(incomingbyte==startChar){
dataBufferIndex = 0; //Initialize our dataBufferIndex variable
storeString = true;
}
if(storeString){
//Let's check our index here, and abort if we're outside our buffer size
//We use our define here so our buffer size can be easily modified
if(dataBufferIndex==DATABUFFERSIZE){
//Oops, our index is pointing to an array element outside our buffer.
dataBufferIndex = 0;
break;
}
if(incomingbyte==endChar){
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
//Our data string is complete. return true
return true;
}
else{
dataBuffer[dataBufferIndex++] = incomingbyte;
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
}
}
else{
}
}
//We've read in all the available Serial data, and don't have a valid string yet, so return false
return false;
if(getSerialString()){
//String available for parsing. Parse it here
}
else{
}
}
includes
defines
global variables
setup function
loop function
extra functions
Could you also press Ctrl+T in the Arduino IDE to auto format the source code.
Then check every indent, every comma, every space, and make the source code look good.
Its essential to nest your braces correctly - the auto-format mentioned is a useful tool to help you with that, but you shouldn't let your code get into a randomly indented state like this anyway, its unreadable. The whole point of indenting is to aid readability.
I didn't know ctrl+T was a thing, thank you for the help. I will try to clean it up and send it again. I am a newbie at arduino and programming in general so I am grateful for any help.
Common compiler errors caused by mismatched brackets:
"does not name a type" or
"expected declaration before" or
"expected unqualified-id before" or
"expected initializer before" or
"expected constructor, destructor, or type conversion before '(' token"
Usually means you forgot a '{' or put in an extra '}' in the previous function. Since all of the open brackets have been closed, the compiler is looking for further global declarations (variables or functions). If it finds something that looks like executable code instead of a global declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.
"a function-definition is not allowed here before '{' token"
(can cause: "'functionName' was not declared in this scope")
Usually means you forgot a '}' or put in an extra '{' in the previous function. Since a set of brackets has not been closed yet the compiler is looking for more code to put in the function. You can't declare a function inside a function so if the compiler finds a function declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.
"expected '}' at end of input"
Usually means you forgot a '}' or put in an extra '{' in the last function in the sketch. Since a set of brackets has not been closed yet, the compiler is looking for more code to put in the function. When it hits the end of the file instead, it emits an error. Make sure that the brackets in the last function are in matching pairs '{' followed by '}'.
"expected primary-expression before '}' token"
Usually means you have an incomplete statement before a '}'. The block statement (between '{' and matching '}') can only contain complete statements. Complete statements always end with ';' (or '}' for a block statement).
I have improved the code, but still have not fixed every problem. I am trying to copy a code from JHaskell's blog. I will aslo send the sender code.
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
// Written by Nick Gammon
// May 2012
#include <Wire.h>
#define DATABUFFERSIZE 6
char dataBuffer[DATABUFFERSIZE + 1]; //Add 1 for NULL terminator
byte dataBufferIndex = 0;
char startChar = '%'; // or '!', or whatever your start character is
char endChar = '&';
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(8, 6);
char startChar = '%'; // or '!', or whatever your start character is
char endChar = '&';
if (getSerialString()) {
//String available for parsing. Parse it here
boolean getSerialString() {
static byte dataBufferIndex = 0;
while (Wire.available() > 0) {
char incomingbyte = Wire.read();
if (incomingbyte == startChar) {
dataBufferIndex = 0; //Initialize our dataBufferIndex variable
storeString = true;
}
if (storeString) {
//Let's check our index here, and abort if we're outside our buffer size
//We use our define here so our buffer size can be easily modified
if (dataBufferIndex == DATABUFFERSIZE) {
//Oops, our index is pointing to an array element outside our buffer.
dataBufferIndex = 0;
break;
}
if (incomingbyte == endChar) {
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
//Our data string is complete. return true
return true;
}
else {
dataBuffer[dataBufferIndex++] = incomingbyte;
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
}
}
else {
}
}
//We've read in all the available Serial data, and don't have a valid string yet, so return false
return false;
}
}
boolean storeString = false; //This will be our flag to put the data in our buffer
while (Wire.available() > 0) {
char incomingbyte = Wire.read();
if (incomingbyte == startChar) {
dataBufferIndex = 0; //Initialize our dataBufferIndex variable
storeString = true;
}
if (storeString) {
//Let's check our index here, and abort if we're outside our buffer size
//We use our define here so our buffer size can be easily modified
if (dataBufferIndex == DATABUFFERSIZE) {
//Oops, our index is pointing to an array element outside our buffer.
dataBufferIndex = 0;
break;
}
if (incomingbyte == endChar) {
//Our data string is complete. Parse it here
storeString = false;
}
else {
dataBuffer[dataBufferIndex++] = incomingbyte;
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
}
}
}
Serial.print(c);
}
Serial.println(" ");
delay(500);
You can't put the getSerialString() function from JHaskell's blog in the middle of another function. No function can be declared inside another function.
I don't think it will work properly if you change Serial.read() to Wire.read(). WIRE messages are generally fixed length.
You are sending a variable-length message containing '%', some digits, and '&'. You might get better results bysending the 'int' rather than a text representation of the 'int':
Wire.send((char *)&b, sizeof b);
Then you can receive it with:
int val;
Wire.requestFrom(8, sizeof val);
if (Wire.available())
{
val = Wire.read() << 8 | Wire.read();
}