weird error in code pls help

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");
  }
}

Hello :slight_smile:

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

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

for (int pingPin = 7; )

I suggest you read:
http://arduino.cc/en/Reference/For

ok taking out the for loop fixed it

but my screen is not display anything

Post your for loop code...

Thats the craziest loop condition Ive ever seen !

Phil

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

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 :slight_smile:

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

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

    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?

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

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.

thank you for pointing that out for me

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.

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.

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

  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.