My lab partner and I are (noobs) trying to get an accelerometer to fade two LEDs from one to the other, and we keep getting closer but we got an error message saying “expected constructor, destructor, or type conversion before ‘(’ token”
can anyone help us out?
{
/*
Accelerometer
A Program which takes input from an accelerometer and outputs the
data to the serial port
Hardware: Parallax MMA7455
Concept Usage: Games controller
*/
#include <Wire.h>
#define ACCELEROMETER 0x1D //Address for Accelerometer
//REGISTERS
#define MODE_CONTROL 0x16 //Mode control register
#define PULSE_DET 0x1B //Pulse detection threshold limit value
#define X_OUT 0x06 //8 bit register containing value for X
#define Y_OUT 0x07 //8 bit register containing value for Y
#define Z_OUT 0x08 //8 bit register containing value for Z
#define DETECTION 0x0A //Detection source register
//VALUES
#define Z_PULSE 0x40 //Pulse detected on Z-axis
#define SENSEVALUE 0x25 //Default sensitivity level
//tilt def.
int tilt = 0;
int leftLEDs = 5;
int leftVal = 0;
int rightLEDs = 6;
int rightVal = 0;
pinMode(pwmPin5, OUTPUT);
pinMode(pwmPin6, OUTPUT);
//required setup function
void setup() {
Wire.begin();
Serial.begin(9600);
accWrite(MODE_CONTROL, SENSEVALUE);
}
//required loop function
void loop() {
Serial.print("X: ");
Serial.print(accRead(X_OUT), DEC);
Serial.print(" Y: ");
Serial.print(accRead(Y_OUT), DEC);
Serial.print(" Z: ");
Serial.println(accRead(Z_OUT), DEC);
delay(100);
{
// get the tilt data
if (tilt < 114 ) {
// process as right tilt
leftVal = map(tilt, 0,114, 0, 255);
analogWrite(leftLEDs,leftVal);
analogWrite(rightLEDs, (leftVal - 255)*-1);
} else {
// process as left tilt //
rightVal = map(tilt, 114, 228, 0, 255);
analogWrite(rightLEDs,rightVal);
analogWrite(leftLEDs, (rightVal - 255)*-1);
}
}
//function to write byte data into a register
void accWrite(byte address, byte data) {
Wire.beginTransmission(ACCELEROMETER);
Wire.send(address);
Wire.send(data);
Wire.endTransmission();
}
//function to read byte data from a register
byte accRead(byte address){
byte val = 0x00;
Wire.beginTransmission(ACCELEROMETER);
Wire.send(address);
Wire.endTransmission();
Wire.requestFrom(ACCELEROMETER, 1);
if(Wire.available() > 0) {
val = Wire.receive();
}
Wire.endTransmission();
return val;
}
// Numbers range in from 46 - 0 and 172 - 255
if(tilt < 50) {
tilt = tilt + 255;
}
// No matter what tilt is, tilt will read from
// 172 -301
tilt = tilt - 172;
// tilt is 0 - 228
if (tilt <= 114) {
// tilt right
}else{
//tilt left
}
your pinMode definitions need to be in void setup(), not before
you didn't declare the pwmPins prior to setup.
go from there.
usually the problem is mismatched {}s, or ()s.
Try the AutoFormat before you compile, will tell you basic things like the mismatched number of () & {}.
/*
Accelerometer
A Program which takes input from an accelerometer and outputs the
data to the serial port
Hardware: Parallax MMA7455
Concept Usage: Games controller
*/
#include <Wire.h>
#define ACCELEROMETER 0x1D //Address for Accelerometer
//REGISTERS
#define MODE_CONTROL 0x16 //Mode control register
#define PULSE_DET 0x1B //Pulse detection threshold limit value
#define X_OUT 0x06 //8 bit register containing value for X
#define Y_OUT 0x07 //8 bit register containing value for Y
#define Z_OUT 0x08 //8 bit register containing value for Z
#define DETECTION 0x0A //Detection source register
//VALUES
#define Z_PULSE 0x40 //Pulse detected on Z-axis
#define SENSEVALUE 0x25 //Default sensitivity level
//tilt def.
int tilt = 0;
int leftLEDs = 5;
int leftVal = 0;
int rightLEDs = 6;
int rightVal = 0;
int pwmPin5 = 5;
int pwmPin6 = 6;
//required setup function
void setup() {
Wire.begin();
Serial.begin(9600);
accWrite(MODE_CONTROL, SENSEVALUE);
pinMode(pwmPin5, OUTPUT);
pinMode(pwmPin6, OUTPUT);
}
//required loop function
void loop() {
Serial.print("X: ");
Serial.print(accRead(X_OUT), DEC);
Serial.print(" Y: ");
Serial.print(accRead(Y_OUT), DEC);
Serial.print(" Z: ");
Serial.println(accRead(Z_OUT), DEC);
delay(100);
}
{
// get the tilt data
if (tilt < 114 ) {
// process as right tilt
leftVal = map(tilt, 0,114, 0, 255);
analogWrite(leftLEDs,leftVal);
analogWrite(rightLEDs, (leftVal - 255)*-1);
} else {
// process as left tilt //
rightVal = map(tilt, 114, 228, 0, 255);
analogWrite(rightLEDs,rightVal);
analogWrite(leftLEDs, (rightVal - 255)*-1);
}
}
//function to write byte data into a register
void accWrite(byte address, byte data) {
Wire.beginTransmission(ACCELEROMETER);
Wire.send(address);
Wire.send(data);
Wire.endTransmission();
}
//function to read byte data from a register
byte accRead(byte address){
byte val = 0x00;
Wire.beginTransmission(ACCELEROMETER);
Wire.send(address);
Wire.endTransmission();
Wire.requestFrom(ACCELEROMETER, 1);
if(Wire.available() > 0) {
val = Wire.receive();
}
Wire.endTransmission();
return val;
}
// Numbers range in from 46 - 0 and 172 - 255
if(tilt < 50) {
tilt = tilt + 255;
}
// No matter what tilt is, tilt will read from
// 172 -301
tilt = tilt - 172;
// tilt is 0 - 228
if (tilt <= 236) {
// tilt right
pwmPin5 (OUTPUT,HIGH)
pwmPin6 (OUTPUT,LOW)
}else{
//tilt left
pwmPin5 (OUTPUT,LOW)
pwmPin6 (OUTPUT,HIGH)
}
we’re still getting an error message, this time saying the same thing but hi-lighting the ‘{’ before “// get the tilt data”
So we are no longer getting an error message, but when uploaded, the LED in pwmPin5 stays on and the LED in pwmPin6 just dimly flickers. so we’re stumped. any suggestions?
/*
*/
#include <Wire.h>
#define ACCELEROMETER 0x1D //Address for Accelerometer
//REGISTERS
#define MODE_CONTROL 0x16 //Mode control register
#define PULSE_DET 0x1B //Pulse detection threshold limit value
#define X_OUT 0x06 //8 bit register containing value for X
#define Y_OUT 0x07 //8 bit register containing value for Y
#define Z_OUT 0x08 //8 bit register containing value for Z
#define DETECTION 0x0A //Detection source register
//VALUES
#define Z_PULSE 0x40 //Pulse detected on Z-axis
#define SENSEVALUE 0x25 //Default sensitivity level
//tilt def.
int tilt = 0;
int leftLEDs = 5;
int leftVal = 0;
int rightLEDs = 6;
int rightVal = 0;
int pwmPin5 = 5;
int pwmPin6 = 6;
//required setup function
void setup() {
Wire.begin();
Serial.begin(9600);
accWrite(MODE_CONTROL, SENSEVALUE);
pinMode(pwmPin5, OUTPUT);
pinMode(pwmPin6, OUTPUT);
}
//required loop function
void loop() {
Serial.print("X: ");
Serial.print(accRead(X_OUT), DEC);
Serial.print(" Y: ");
Serial.print(accRead(Y_OUT), DEC);
Serial.print(" Z: ");
Serial.println(accRead(Z_OUT), DEC);
delay(100);
// get the tilt data
if (tilt < 114 ) {
// process as right tilt
leftVal = map(tilt, 0,114, 0, 255);
analogWrite(leftLEDs,leftVal);
analogWrite(rightLEDs, (leftVal - 255)*-1);
} else {
// process as left tilt //
rightVal = map(tilt, 114, 228, 0, 255);
analogWrite(rightLEDs,rightVal);
analogWrite(leftLEDs, (rightVal - 255)*-1);
}
// Numbers range in from 46 - 0 and 172 - 255
if(tilt < 50) {
tilt = tilt + 255;
}
// No matter what tilt is, tilt will read from
// 172 -301
tilt = tilt - 172;
// tilt is 0 - 228
if (tilt <= 236) {
// tilt right
analogWrite(5, 255);
analogWrite(6, 0);
}else{
//tilt left
analogWrite(5, 0);
analogWrite(6, 255);
}
}
//function to write byte data into a register
void accWrite(byte address, byte data) {
Wire.beginTransmission(ACCELEROMETER);
Wire.send(address);
Wire.send(data);
Wire.endTransmission();
}
//function to read byte data from a register
byte accRead(byte address){
byte val = 0x00;
Wire.beginTransmission(ACCELEROMETER);
Wire.send(address);
Wire.endTransmission();
Wire.requestFrom(ACCELEROMETER, 1);
if(Wire.available() > 0) {
val = Wire.receive();
}
Wire.endTransmission();
return val;
}
It appears form that code that you know how to use Serial.print() to output debug information. Why don't you output leftVal and rightVal, too? While your at it, why not output tilt, every where that it is set?
Pretty much the root of your problem is right here.
int tilt = 0;
Where does tilt ever get assigned a meaningful value based on the accelerometer readings?