-1

this is not my code this is what i found on makersguide i tried to use the same but it doesn't work

#include <Servo.h>

// Define Trig and Echo pin: #define trigPin 10 #define echoPin 9 // Define variables: long duration; int distance; void setup() { // Define inputs and outputs: pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); } void loop() { // Clear the trigPin by setting it LOW: digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin,HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds: duration = pulseIn(echoPin, HIGH); // Calculate the distance: distance = duration * 0.034 / 2; // Print the distance on the Serial Monitor (Ctrl+Shift+M): Serial.print("Distance = "); Serial.print(distance); Serial.println(" cm"); delay(50); }

// Include the servo library: #include <Servo.h> // Create a new servo object: Servo myservo; // Define the servo pin: #define servoPin 9 // Create a variable to store the servo position: int angle = 0; voidsetup() { // Attach the Servo variable to a pin: myservo.attach(servoPin); } void loop(){ myservo.write(90); delay(1000); myservo.write(180); delay(1000); myservo.write(0); delay(1000); // Sweep from 0 to 180 degrees: for (angle = 0; angle <= 180; angle += 1) { myservo.write(angle); delay(0); } for (angle = 180; angle <= 0; angle -= 1) { myservo.write(angle); delay(0);

} delay(1000); }

NEXE
  • 35
  • 5
  • "Doesn't work" doesn't tell us much. What happens? Anything? Maybe it's a wiring issue, which we can't tell by reading your question. Maybe it doesn't compile? You seem to have two loop functions, and a voidsetup whatever that is. – Nick Gammon Nov 27 '20 at 05:33
  • it says that my void loop is previously defined – NEXE Nov 27 '20 at 05:37
  • and it also says that ISO C++ forbids declaration of void setup with no type [-fpermissive] in function of void loop( ) – NEXE Nov 27 '20 at 05:49
  • It actually said "warning: ISO C++ forbids declaration of 'voidsetup' with no type [-fpermissive]". Notice that you omitted the space between "void" and "setup". That's why we ask you to copy and paste the error message, not just paraphrase them. – Nick Gammon Nov 27 '20 at 05:58
  • For combining sketches, look at this question. There are also links to similar questions. – chrisl Nov 27 '20 at 07:59

1 Answers1

0

Your code has two "loop" functions in it. You cannot have two functions of the same name (and with the same argument list, in your case with no arguments) in the one compilation unit (piece of code).

It looks very much like you have combined two pieces of code you found into one and then tried to compile that. You can't do that. Choose one or the other. In the second "half" you incorrectly have "voidsetup" which should be "void setup".

Next time you post a question, and if the question is about an error message, please copy and paste the error message. We aren't mind readers. You can see what is on your screen, we can't.

See: How do I ask a good question about the Arduino?

Nick Gammon
  • 38,184
  • 13
  • 65
  • 124