// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
#include <Servo.h>
void setup()[
Serial.begin(9600)
Servo myservo;
int pos = 0;
myservo.attach(9);
]
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(2);
if(sensorValue<600){
{
for(pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=0; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
}
error:
exit status 1
array bound is not an integer constant before '{' token
I tried to combine code for a servo motor and code for a muscle sensor, but i dont really know what it does, so I just tried to just combine and fix errors on the way, but I didnt know what this error was.
Thanks, i fixed that, but i have another error.
Code:
// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
#include <Servo.h>
void setup()
{
Serial.begin(9600)
;Servo myservo;
int pos = 0;
myservo.attach(9);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(2);
if (sensorValue < 600) {
{
for (pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1)
{
myservo.write(pos);
delay(15);
}
}
}
In simple: scope is {here} anything here is not in {this scope}.
If you don’t declare something inside the block of code contained within the {}s it will throw up this error, with the exception of a global declared variable
You might want to start googling things as you don’t understand what {} are or scope or basic syntax. You can’t just mash code together you need to understand what you are doing. Look up some beginner tutorials
You already have in setup but it is only available to the setup function because it is contained within its {scope}. You can move it outside all functions to make global but it really depends on what you want it to do.
Look up basic syntax. It is in the link I posted. You can’t just write any old thing down it needs to be in the correct syntax. That means all the {s must not be ( or [ and ; must be in the right place etc etc. just look at the arduino language reference for examples and double check each thing you do to make sure it matches the syntax of the example exactly
There's a lot going on in this code, including that you have used the same pin for both the servo and led. Looks like you took two different examples and mashed them together before carefully considering what each line does.
Also, you may find it easier to get rid of so many extra spaces between code lines.
Did you know when you click immediately to the right of a curly brace, it shows the matching curly brace by putting a little box around it? Your curly braces in loop didn't work, they'll need adjusting. Also, you didn't declare your led pin as an output in setup().
Start over. First get the dimming LED working. Then, once you have that, try to add in a servo, on a different pin, and make it global by declaring Servo myservo; with the other global definitions.
This should at least get you started, have a look in serial monitor and watch what's going on to try to get the result you're after. Also, switch the servo pin to pin 10 like in the following revision. This is only to get you going, it will compile and you'll have to troubleshoot from here.
#include <Servo.h>
Servo myservo;
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
int pos = 0;
void setup() {
Serial.begin(9600);
myservo.attach(10);
pinMode(analogInPin, INPUT);
pinMode(analogOutPin, OUTPUT);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(2);
if (sensorValue < 600) {
for (pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1)
{
myservo.write(pos);
delay(15);
}
}
}
The fact that you are asking about every compiler error indicates you are over your head. Even if you get a compile/build with no errors then you have to debug the logic.
You need to review basic C++ program structure, syntax, control structures, and variable scoping.