Can someone help? I am new and I can't seem to get my functions to work an compile

I just started and I'm trying to get some functions to work, any help would be great.
Here is the code I have:

/*Make a code that has 3 functions of Safe, Threatened, and in danger
* The 3 functions must all do something different
*/
 
//Create variables
 
int danger;
danger = 
 
#include <LiquidCrystal_I2C.h>
 
#include <Wire.h>
 
int trigger=32;
 
int echo=33;
 
unsigned long int duration=0;
 
float distance = 0.0;
 
int buzzer=2;
 
#include <Servo.h>
 
Servo rightServo;
 
Servo leftServo;
 
//Include libraries
#include <Adafruit_NeoPixel.h>
 
#ifdef __AVR__
 
  #include <avr/power.h>
  
#endif
 
#define PIN 9
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);
 
LiquidCrystal_I2C lcd(0x27, 16, 2);
 
void setup() {
  //Setup all of the outputs and inputs
  strip.begin();
  
  strip.show();
  
  pinMode(trigger,OUTPUT);
  
  pinMode(echo,INPUT);
  
  Serial.begin(9600);
 
  pinMode(buzzer,OUTPUT);
  
  //initialize lcd screen
  lcd.init();
  // turn on the backlight
  lcd.backlight();
 
  //Attach the servos
  rightServo.attach(7);
  
  leftServo.attach(6);
 
}
void loop() {
 
  //Turn us sensor off
  digitalWrite(trigger,LOW);
  
  delayMicroseconds(2);
 
  digitalWrite(trigger,HIGH);
  
  delayMicroseconds(10);
  
  digitalWrite(trigger,LOW);
 
  duration = pulseIn(echo,HIGH);
  
  distance = ((float)(duration))/58.2;
 
  Serial.print("Distance=");
  
  Serial.print(distance);
  
  Serial.println("cm");
 }
  //Danger function call
if (distance <10){
  
    danger()
    
}
 
//Threatened function call
  else if ((distance >10)&&(distance <25)){
    
    threatened()
    
  }
 
  //Safe function call
  else{
    
    safe()
    
}
// ColowWipe function definition
void  colorWipe(uint32_t c, uint8_t wait) {
  
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    
    strip.setPixelColor(i, c);
    
    strip.show();
    
    delay(wait);
    
  }
}
 
//Safe function definition
void  safe()
  
  colorWipe(strip.Color(247, 0, 255), 50);
  
    lcd.setCursor(1,1);
    
  lcd.print("CHILLAXED ");
    
    rightServo.write(90);
    
    leftServo.write(90);
    
 
 
//Threatened functon definition
void  threatened()
  
  colorWipe(strip.Color(0, 230, 255), 50);
  
    lcd.setCursor(1,1);
    
  lcd.print("THREATENED");
  
  tone(buzzer,500);
  
    rightServo.write(0);
    
    leftServo.write(0);
    
    delay(2900);
    
    rightServo.write(0);
    
    leftServo.write(180);
    
    delay(2000);
    
    rightServo.write(90);
    
    leftServo.write(90);
 
 
//danger function definition
void danger()
  
  colorWipe(strip.Color(255, 220, 0), 50);
  
  rightServo.write(0);
  
  leftServo.write(180);
  
  lcd.setCursor(1,1);
 
  lcd.print("ATTACK ");
  
  tone(buzzer,300);
 
 
}

Help us help you.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Please post a schematic.

Please post an image of your project.

Please describe the problem better then you just did.

This is a school assignment, right?

equals what ?


please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

Sorry My first post.

Let me try again, I'm trying to help my nephew with a project and he wants to have an LCD show a few words when his project gets to close to an obstacal. Here is the code with three functions- danger,threatend, and safe. I am sure it is formatting in the code but not sure what is missing or needs to be changed.

/ /*Make a code that has 3 functions of Safe, Threatened, and in danger
* The 3 functions must all do something different
*/
 
//Create variables
 
int danger;
danger = 
 
#include <LiquidCrystal_I2C.h>
 
#include <Wire.h>
 
int trigger=32;
 
int echo=33;
 
unsigned long int duration=0;
 
float distance = 0.0;
 
int buzzer=2;
 
#include <Servo.h>
 
Servo rightServo;
 
Servo leftServo;
 
//Include libraries
#include <Adafruit_NeoPixel.h>
 
#ifdef __AVR__
 
  #include <avr/power.h>
  
#endif
 
#define PIN 9
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);
 
LiquidCrystal_I2C lcd(0x27, 16, 2);
 
void setup() {
  //Setup all of the outputs and inputs
  strip.begin();
  
  strip.show();
  
  pinMode(trigger,OUTPUT);
  
  pinMode(echo,INPUT);
  
  Serial.begin(9600);
 
  pinMode(buzzer,OUTPUT);
  
  //initialize lcd screen
  lcd.init();
  // turn on the backlight
  lcd.backlight();
 
  //Attach the servos
  rightServo.attach(7);
  
  leftServo.attach(6);
 
}
void loop() {
 
  //Turn us sensor off
  digitalWrite(trigger,LOW);
  
  delayMicroseconds(2);
 
  digitalWrite(trigger,HIGH);
  
  delayMicroseconds(10);
  
  digitalWrite(trigger,LOW);
 
  duration = pulseIn(echo,HIGH);
  
  distance = ((float)(duration))/58.2;
 
  Serial.print("Distance=");
  
  Serial.print(distance);
  
  Serial.println("cm");
 }
  //Danger function call
if (distance <10){
  
    danger()
    
}
 
//Threatened function call
  else if ((distance >10)&&(distance <25)){
    
    threatened()
    
  }
 
  //Safe function call
  else{
    
    safe()
    
}
// ColowWipe function definition
void  colorWipe(uint32_t c, uint8_t wait) {
  
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    
    strip.setPixelColor(i, c);
    
    strip.show();
    
    delay(wait);
    
  }
}
 
//Safe function definition
void  safe()
  
  colorWipe(strip.Color(247, 0, 255), 50);
  
    lcd.setCursor(1,1);
    
  lcd.print("CHILLAXED ");
    
    rightServo.write(90);
    
    leftServo.write(90);
    
 
 
//Threatened functon definition
void  threatened()
  
  colorWipe(strip.Color(0, 230, 255), 50);
  
    lcd.setCursor(1,1);
    
  lcd.print("THREATENED");
  
  tone(buzzer,500);
  
    rightServo.write(0);
    
    leftServo.write(0);
    
    delay(2900);
    
    rightServo.write(0);
    
    leftServo.write(180);
    
    delay(2000);
    
    rightServo.write(90);
    
    leftServo.write(90);
 
 
//danger function definition
void danger()
  
  colorWipe(strip.Color(255, 220, 0), 50);
  
  rightServo.write(0);
  
  leftServo.write(180);
  
  lcd.setCursor(1,1);
 
  lcd.print("ATTACK ");
  
  tone(buzzer,300);
 
 
}

Yes it is it is for my nephew and I'm just trying to help him out. I reposted the code down below if you can help me at all it would be great.

you need to terminate that line of code with a semicolon.

I guess you have been getting compiler errors. It helps us if you say what is right and wrong, whether it compiles and if it does what you want.

HTH

a7

Functions need braces too.


void danger()
  { // brace   <β€”β€”β€”<<<<  opening brace
  colorWipe(strip.Color(255, 220, 0), 50);
  
  rightServo.write(0);
  
  leftServo.write(180);
  
  lcd.setCursor(1,1);
 
  lcd.print("ATTACK ");
  
  tone(buzzer,300);
}  //<β€”β€”β€”<<<<   closing brace

This compiles for an UNO ......wether does what you want is something else

/*Make a code that has 3 functions of Safe, Threatened, and in danger
  The 3 functions must all do something different
*/

//Create variables

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
int trigger = 32;
int echo = 33;
unsigned long int duration = 0;
float distance = 0.0;
int buzzer = 2;

#include <Servo.h>
Servo rightServo;
Servo leftServo;
//Include libraries
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 9
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  //Setup all of the outputs and inputs
  strip.begin();
  strip.show();
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(9600);
  pinMode(buzzer, OUTPUT);
  //initialize lcd screen
  lcd.init();
  // turn on the backlight
  lcd.backlight();
  //Attach the servos
  rightServo.attach(7);
  leftServo.attach(6);
}
void loop() {

  //Turn us sensor off
  digitalWrite(trigger, LOW);
  delayMicroseconds(2);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);
  duration = pulseIn(echo, HIGH);
  distance = ((float)(duration)) / 58.2;
  Serial.print F("Distance=");
  Serial.print(distance);
  Serial.println F("cm");
  //Danger function call
  if (distance < 10)
  {
    danger();
  }
  //Threatened function call
  else if ((distance > 10) && (distance < 25))
  {
    threatened();
  }
  //Safe function call
  else
  {
    safe();
  }
}
// ColowWipe function definition
void  colorWipe(uint32_t c, uint8_t wait)
{
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}
//Safe function definition
void  safe()
{
  colorWipe(strip.Color(247, 0, 255), 50);
  lcd.setCursor(1, 1);
  lcd.print F("CHILLAXED ");
  rightServo.write(90);
  leftServo.write(90);
}
//Threatened functon definition
void  threatened()
{
  colorWipe(strip.Color(0, 230, 255), 50);
  lcd.setCursor(1, 1);
  lcd.print F("THREATENED");
  tone(buzzer, 500);
  rightServo.write(0);
  leftServo.write(0);
  delay(2900);
  rightServo.write(0);
  leftServo.write(180);
  delay(2000);
  rightServo.write(90);
  leftServo.write(90);
}
//danger function definition
void danger()
{
  colorWipe(strip.Color(255, 220, 0), 50);
  rightServo.write(0);
  leftServo.write(180);
  lcd.setCursor(1, 1);
  lcd.print F("ATTACK ");
  tone(buzzer, 300);
}

I doubt that your nephew was just thrown into the class yesterday. He/she is probably able to correct many of the errors in the sketch (that were deliberately placed there by the instructor). I strongly suggest that you allow him/her to make an attempt at fixing it up first. Then re-post it, along with any issues or problems that he/she experiences with the updated sketch.

Otherwise you are letting the student retrograde to the first class/exercise. That is not the way to help someone.

Nope it's all his code, no instructor errors but hey if you don't want to help that is fine I'll figure it out. Have a nice night.

Why did you get the impression that I don't want to help? I just don't want to help someone cheat.

From what you say, I would guess that it is far too late in the course to really help your nephew much. Those errors would have been dealt with and overcome in the first few elementary lessons. What happened?

the due date is either today or tomorow :nerd_face:

An important thing to learn:
The body of a function starts with '{' and ends with the matching '}'.

Many of the functions are missing both '{' and '}'.

The loop() function has the matching '}' too early so there is a bunch of code that should be in the loop() body but isn't.

You can't use the name 'danger' for both a global variable and a function. Get rid of the global variable since it is not used.

Statements, including function calls, end with ';'. The ';' is missing on the lines that call danger(), threatened(), and safe().

After fixing those mistakes, the code compiles without error.

1 Like

thanks John.

Man you guys are just brutal, I just wanted help with syntax, if it does what he wants fine if not fine. If you don't want to help just move on. The project he is working on is not due for 3 months I was just hoping I could help him out. Have a good day sir.

3 months is a long enough time for a student to independently catch up with a project like this. I always like to see a student progress. Typically, the best way for that to happen is by following the course materials and instruction at the pace it's offered, and doing independent research of any topics that can support that.

These are all things that your nephew can and should do. It's far too early to swoop in and "help" by finding internet helpers to fix his code. It will be counterproductive because then he will not have the experience of fixing it himself.

If that sounds brutal to you, understand that I personally consider what would be the most effective course of action for a student to take. There are a lot of ways a knowledgeable person can contribute to someone's education, but completing assignments for them, is not one.

However to comply with your request to move on, I will block your posts so I don't see them any more.

3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.