system
March 22, 2013, 12:07am
1
i keep getting the following errors
sketch_mar21a:60: error: expected unqualified-id before 'for'
sketch_mar21a:60: error: expected unqualified-id before ')' token
heres my code all i want is for the ping sensor to log data to the sd card
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_DC 4
#define OLED_CS 6
#define OLED_CLK 5
#define OLED_MOSI 3
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const int chipSelect = 10;
const int pingPin = 7;
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
}
void loop() {
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
String dataString = "";
for (int pingPin = 7; ) {
int sensor = digitalRead(pingPin);
dataString += String(sensor);
if (pingPin < 2) {
dataString += ",";
}
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error opening datalog.txt");
}
}
guix
March 22, 2013, 12:17am
2
Hello
You can't have such code outside of a function body. Maybe this part of code should be inside the loop() function?
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_DC 4
#define OLED_CS 6
#define OLED_CLK 5
#define OLED_MOSI 3
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const int chipSelect = 10;
const int pingPin = 7;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
}
void loop()
{
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
String dataString = "";
for (int pingPin = 7; )
{
int sensor = digitalRead(pingPin);
dataString += String(sensor);
if (pingPin < 2)
{
dataString += ",";
}
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else
{
Serial.println("error opening datalog.txt");
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
system
March 22, 2013, 12:26am
3
i just tried the code and got this error
sketch_mar21b.ino: In function 'void loop()':
sketch_mar21b:54: error: expected primary-expression before ')' token
sketch_mar21b:54: error: expected `;' before ')' token
cmiyc
March 22, 2013, 12:29am
4
for (int pingPin = 7; )
I suggest you read:
http://arduino.cc/en/Reference/For
system
March 22, 2013, 12:34am
5
ok taking out the for loop fixed it
system
March 22, 2013, 12:38am
6
but my screen is not display anything
Post your for loop code...
Thats the craziest loop condition Ive ever seen !
Phil
system
March 22, 2013, 12:56am
8
i was trying to use the code from the ping sample along with the code for the datalogger and lcd screen since im still fairly new to arduino
system
March 22, 2013, 1:51am
9
ok here is my new code but my lcd screen is not working and i have tested the screen by itself and it does work but for some reason not with my code can any one show me what mistakes im making?
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_DC 4
#define OLED_CS 6
#define OLED_CLK 5
#define OLED_MOSI 3
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const int chipSelect = 10;
const int pingPin = 7;
void setup()
{
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC);
display.display();
delay(2000);
display.clearDisplay();
display.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect))
{
display.println("Card failed, or not present");
return;
}
display.println("card initialized.");
}
void loop()
{
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
display.print("in, ");
Serial.print(cm);
display.print("cm");
Serial.println();
delay(100);
String dataString = "";
int sensor = digitalRead(pingPin);
dataString += String(sensor);
if (pingPin < 2)
{
dataString += ",";
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else
{
display.println("error opening datalog.txt");
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
Hi MillerJLee98579
I think you need to take a bit of time to learn some basic 'c' coding. At least make sure you understand what each statement you write is doing. Comment your code so you can follow it.
I've re-jigged it somewhat, still dont know why your testing pingPin for a value over 2, its never going to be anything other than 7. I suspect your trying to test something else to do with the format of your data string.
Dont need the 'for' loop, your whole sketch is in one big loop anyway. Take a look at the changes and if you can figure out what it is your trying to achieve, ask again. Programming is just logical, you need to understand the basics first. Doesnt look like what your trying to do is too difficult, keep plugging at it
Phil
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_DC 4
#define OLED_CS 6
#define OLED_CLK 5
#define OLED_MOSI 3
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const int chipSelect = 10;
const int pingPin = 7;
File dataFile;
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
}
void loop() {
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
String dataString = "";
int sensor =0;
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
sensor = digitalRead(pingPin);
dataString += String(sensor);
dataFile = SD.open("datalog.txt", FILE_WRITE);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}//if
else {
Serial.println("error opening datalog.txt");
}//else
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}// loop
long microsecondsToInches(long microseconds)
{
return (microseconds / 74 ) / 2;
}//microsecondsToInches
long microsecondsToCentimeters(long microseconds)
{
return (microseconds / 29) / 2;
}//microsecondsToCentimeters
system
March 22, 2013, 2:11am
11
i have 2 force sensors to add to this and 2 continuous rotation servos once i get everything figured out.. the lcd screen is supposed to display the messages about the card slot as well as displaying distance from the ping sensor
system
March 22, 2013, 5:47am
12
int sensor = digitalRead(pingPin);
dataString += String(sensor);
if (pingPin < 2)
{
dataString += ",";
}
Why does the pin number make any difference to whether you append a comma to the String?
system
March 22, 2013, 6:08am
13
i wound up scrapping that code nd starting over but im still getting errors so here is the new code
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(3, 5, 6, 7, 8, 9);
const int chipSelect = 4;
const int pingPin = 2;
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
lcd.print("Initializing SD card...");
pinMode(10, OUTPUT);
delay(250);
lcd.clear();
delay(250);
if (!SD.begin(chipSelect)) {
lcd.println("Card failed.");
// don't do anything more:
return;
}
lcd.println("card initialized.");
}
void loop() {
String dataString = "";
int sensor = digitalRead(pingPin);
dataString += String(sensor);
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
else {
lcd.println("error opening datalog.txt");
}
}
}
and here are the errors
sketch_mar21b:32: error: expected unqualified-id before 'if'
sketch_mar21b:38: error: expected unqualified-id before 'else'
sketch_mar21b:41: error: expected declaration before '}' token
and once again i do appologize for my poor coding skills i missed alot of programming labs due to a car accident last month
system
March 22, 2013, 6:14am
14
I'll make this as simple as possible. All executable code MUST be in a function. loop() is a function. setup() is a function.
This code:
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
else {
lcd.println("error opening datalog.txt");
}
}
}
is NOT in a function.
You can't just stick code anywhere.
Do yourself a big favor. Put EVERY { on a new line, all by itself. Use Tools + Auto Format.
system
March 22, 2013, 6:22am
15
thank you for pointing that out for me
system
March 22, 2013, 6:54am
16
ok i kinda got it working but now all my lcd screen shows is a blinking cursor and no messages
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(3, 5, 6, 7, 8, 9);
const int chipSelect = 4;
const int pingPin = 2;
File dataFile;
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
lcd.print("Initializing SD card...");
pinMode(10, OUTPUT);
delay(250);
lcd.clear();
delay(250);
if (!SD.begin(chipSelect)) {
lcd.println("Card failed.");
// don't do anything more:
return;
}
lcd.println("card initialized.");
}
void loop() {
lcd.clear();
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
String dataString = "";
int sensor =0;
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
sensor = digitalRead(pingPin);
dataString += String(sensor);
dataFile = SD.open("datalog.txt", FILE_WRITE);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}//if
else {
Serial.println("error opening datalog.txt");
}//else
Serial.print(inches);
lcd.print("in, ");
Serial.print(cm);
lcd.print("cm");
delay(100);
}
long microsecondsToInches(long microseconds)
{
return (microseconds / 74 ) / 2;
}//microsecondsToInches
long microsecondsToCentimeters(long microseconds)
{
return (microseconds / 29) / 2;
}//microsecondsToCentimeters
My first suspicion would be wiring. I'd suggest that you take a copy of that sketch, remove everything from loop and just print your first message to the lcd in setup. Until that's working, there's not much point executing the rest.
system
March 22, 2013, 3:14pm
18
The File::println() method is perfectly capable of writing an int to a file, as a string. It does not need for you to waste resources wrapping the int in a String, requiring it to unwrap the String.
system
March 22, 2013, 5:17pm
19
i tripled checked all the wiring and had it working from setup and now all i have is a blinking cursor. since this will not be connected to a computer it needs to have the lcd screen displaying exactly the same information that its data logging
Arrch
March 22, 2013, 7:11pm
20
Serial.print(inches);
lcd.print("in, ");
Serial.print(cm);
lcd.print("cm");
This seems like an odd thing to do: split labels and values between Serial and LCD. I get the feeling either the Serials should also be lcds or lcds should be Serials.