-2

Was trying to work some code that I found online to suit my needs and it continues to show the same error 'setup' does not name a type. I have tried setting it to void, Void as well as rewriting the code but still the same issue arises and I was wondering how to fix this issue.

This program will at some point compile data from 3 different sensors and do its various calculations within these separate loops and print to serial in a table that can then be saved or exported. The only sensor attached at the moment is a ultrasonic sensor that measures the distance it is from a surface. (last 2 groups of code have a basic print function just so the loop is occupied)

Here is the code:

#include <CapacitiveSensor.h>

long timeCounter; // milliseconds counter  
long timePassed; // The actual milliseconds time 
int trigPin = 2;
int echoPin = 3; 

long delay1 = 10;    // ms for cycle of event 1  
long delay2 = 5;     // ms for cycle of event 2  
long delay3 = 25;    // ms for cycle of event 3  
long delay4 = 10;

// Counters for the number of triggered events before the  
// corresponding event starts  
int countEvent1, countEvent2, countEvent3, countEvent4;  

int TRIGGER = 5; // ms to trigger the milliseconds timer  

int MAX1 = 2; // Number or triggered milliseconds to start event1  
int MAX2 = 1; // Number or triggered milliseconds to start event2  
int MAX3 = 5; // Number or triggered milliseconds to start event3  
int MAX4 = 2;

// Initialisation  
setup {  
     timeCounter = millis(); // read the actual internal time  
     timePassed = 0; // we start with no time passed  

     countEvent1 = 0;  
     countEvent2 = 0;  
     countEvent3 = 0;
     countEvent4 = 0; 
} 

// Infinite cycle  
loop() {  

     // Read the actual time  
     timePassed = millis() - timeCounter;  
     // The smallest unit of time that should trigger an event is for event 2 (5 ms)  
     // so our trigger should count this smaller unit  

     // Check if the time passed after last reading is the trigger time  
     if(timePassed >= TRIGGER) {   
          if(++countEvent1 == MAX1){  
               // Reset the trigger and start the event  
               event1();  
               countEvent1 = 0;  
          } // Event 1   

          // It's time to manage the events  
          if(++countEvent2 == MAX2){  
               // Reset the trigger and start the event  
               event2();  
               countEvent2 = 0;  
          } // Event2 

          // It's time to manage the events  
          if(++countEvent3 == MAX3){  
               // Reset the trigger and start the event  
               event3();  
               countEvent3 = 0;  
          } // Event3

          if(++countEvent4 == MAX4){  
               // Reset the trigger and start the event  
               event4();  
               countEvent4 = 0;  
          } // Event4 

     } // Trigger 

} // LOOP  

// ============ Event functions ============== 

event1() {  
  long duration, distance;
  digitalWrite(trigPin,HIGH);
  delay(100);
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
  distance =(duration/2)/29.1;
}  

event2() {  
Serial.print("work")
}  

event3() {   
Serial.print("work")
}  

I am very new to coding so there will be stupid mistakes hidden amongst this I can guarantee it.

  • 3
    You're just forgetting the smooth brackets after void setup. It should be void setup () { – Carrot M Mar 23 '17 at 20:00
  • 2
    You should learn the basics of a language's syntax before writing programs in it... – Majenko Mar 23 '17 at 20:11
3

All your functions (including setup and loop) need a type as part of the declaration. This for example:

event2() {  
Serial.print("work")
}  

Should be:

void event2() {  
  Serial.print("work");
  }

Note also the semicolon after the Serial.print function call.

I have a page that explains the basics.

On that page is a diagram that breaks down what a function declaration looks like:

Function definitions

3

You have a problem with function definition. Let me explain with an example.

int kPinLed = 8; // LED - Pin 8

void setup()
{
  pinMode(kPinLed, OUTPUT); // LED as output
}

void loop()
{
  ledFun(); //Create LED Function
}

//Now write function's code with type, here "void"

void ledFun()
{
  digitalWrite(kPinLed, HIGH); //LED ON
  delay(1000);
  digitalWrite(kPinLed, LOW);  //LED OFF
  delay(1000);
}

Same things you have to do with your code. Just write properly function's type. I hope this will help you.

1

Function declarations start with a type:

void setup() {

}

void loop() {

}

void means the function doesn't return a value.

0

Yeah you never defined setup or loop which need the void before them.

void setup(){

}



void loop(){

} 
0

error 'setup' does not name a type. I have tried setting it to void

the correct typing of it is this:

void setup(void) {
...
}

void loop(void) {
...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.