Please help me with these bits of code, I am unable to tell the problems
#include <Wire.h>
#include <math.h>
int sensorpin = A0;
void setup()
{
pinMode(12, OUTPUT); // sets the LED at pin 12
double c = 4*(sq(PI)); // declaring the constant 4 pi^2
double L = 48;
Wire.begin(2); // initializes the i2c bus
}
void loop()
{
int val = analogRead(sensorpin);
digitalWrite(12, HIGH);
if(val > 200)
{
do
{
double T = 2*(micros()/1000);
}
while (val < 200);
}
int T2 = (int) T * (int) T;
double g = double c* double L/T2;
delay(25);
byte Tp = (byte) T2;
byte gr = (byte) g;
byte test[2] = {
Tp, gr };
Wire.write(test, 2);
}
and the other
#include <LiquidCrystal.h> // The LCD library
#include <Wire.h> // The i2c bus library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Pins used
void setup()
{
lcd.begin(16, 2);
Wire.begin(1);
}
void loop()
{
Wire.requestFrom(2, 96);
recieveEvent();
byte thisArray[2];
lcd.print("The period =");
lcd.print(thisArray[1]);
lcd.setCursor(0, 1);
lcd.print("The value of g =");
lcd.print(thisArray[2]);
}
void recieveEvent()
{
while(Wire.available() >0)
{
for(int p = 0; p<2; p++)
{
byte thisArray[p] = Wire.read();
}
}
}
I'm trying to use one uno with an IR light gate that times the period of a pendulum swinging and then sending the info to the other (second bit of code) to display on a 16x2 LCD. Any help?
Slave.ino: In function 'void loop()':
Slave:34: error: 'T' was not declared in this scope
Slave:35: error: expected primary-expression before 'double'
Slave:35: error: expected ',' or ';' before 'double'
and in the second
master.ino: In function 'void recieveEvent()':
master:40: error: variable-sized object 'thisArray' may not be initialized
You only have to declare a variable once. Preferably at the beginning of your program, unless it's local to a function (which, by the way, is not a bad thing). You already did it with some variables but they are declared inside setup().
Once a variable is declared, like:
int x = 77;
you should reference it without a declarator from that point on, like:
x = 66;
The compiler remembers what data type you asked for.
Slave.ino: In function 'void loop()':
Slave:34: error: 'T' was not declared in this scope
Slave:35: error: 'c' was not declared in this scope
Slave:35: error: 'L' was not declared in this scope
Declare a variable only once.
Declare it in the function that is used.
Declare it before you use it.
If it is used in multiple functions, declare it outside a function (outside setup() or loop()).
Sorry I just fixed the declaration problem;
would you be able to help with this bit of code ?
/*
Ok so, this arduino receives data sent from the other one and
dosen't do anything more than display it. We needed two arduinos
for the number of pins the lcd takes up
*/
#include <LiquidCrystal.h> // The LCD library
#include <Wire.h> // The i2c bus library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Pins used
void setup()
{
lcd.begin(16, 2);
Wire.begin(1);
}
void loop()
{
Wire.requestFrom(2, 96);
recieveEvent();
byte thisArray[2];
lcd.print("The period =");
lcd.print(thisArray[1]);
lcd.setCursor(0, 1);
lcd.print("The value of g =");
lcd.print(thisArray[2]);
}
void recieveEvent()
{
while(Wire.available() >0)
{
for(int p = 0; p<2; p++)
{
byte thisArray[p] = Wire.read();
}
}
}
as it gives this error:
master.ino: In function 'void recieveEvent()':
master:40: error: variable-sized object 'thisArray' may not be initialized