Go Down

Topic: weird error in code pls help (Read 1 time) previous topic - next topic

MillerJLee98579

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
Code: [Select]
#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

Hello :)

You can't have such code outside of a function body. Maybe this part of code should be inside the loop() function?

Code: [Select]

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

MillerJLee98579

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

James C4S

Quote
Code: [Select]
for (int pingPin = 7; )

I suggest you read:
http://arduino.cc/en/Reference/For
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

MillerJLee98579

ok taking out the for loop fixed it

MillerJLee98579

but my screen is not display anything

SurfCracker

Post your for loop code...

Thats the craziest  loop condition Ive ever seen !

Phil

MillerJLee98579

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

MillerJLee98579

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?

Code: [Select]
#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;
}

SurfCracker

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

Code: [Select]

#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

MillerJLee98579

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

PaulS

Code: [Select]
    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?

MillerJLee98579

i wound up scrapping that code nd starting over but im still getting errors so here is the new code
Code: [Select]
#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

PaulS

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:
Code: [Select]
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.

MillerJLee98579

thank you for pointing that out for me

Go Up