i am trying to use a tilt sensor with the example debounce program to control a servo, i have included the sketch, i am having trouble making the digital reading from the tilt sensor control the analog servo
tilt_sens_servo_debounce1.pde (2.11 KB)
i am trying to use a tilt sensor with the example debounce program to control a servo, i have included the sketch, i am having trouble making the digital reading from the tilt sensor control the analog servo
tilt_sens_servo_debounce1.pde (2.11 KB)
Your code is short enough to include in your post, using the # button. Try again.
// constants won't change. They're used here to
// set pin numbers:
#include <Servo.h>
const int buttonPin = 12; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
Servo myservo;
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int pos = 0;
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 200; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(12, HIGH); // pull-up
myservo.attach(9);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}
// set the LED using the state of the button:
digitalWrite(ledPin, buttonState);
if(buttonState==LOW); {
{pos = 0}; {pos < 180}; {pos += 1}; // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
Moderator edit: [code] [/code]
tags added.
You have some improperly posted code (I did say to use the code button, the one with the # sign on it), and a goal (use a tilt sensor to control a servo), but no description of what is working or what is not working.
Use Serial.begin() in setup, and Serial.print() and Serial.println() in loop(), to see what is happening.
long debounceDelay = 200; // the debounce time; increase if the output flickers
I don't believe I've ever seen a servo flicker.
if(buttonState==LOW); {
{pos = 0}; {pos < 180}; {pos += 1}; // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Putting each { and } would go a long way towards showing you what your problem is.
if(buttonState==LOW); // The semicolon on the end IS the body of this
// if statement, and is a no-operation statement
{
{
pos = 0
};
{
pos < 180
};
{
pos += 1
}; // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Missing semi-colons all over the place. Extra { and } where not needed. Was this supposed to be a for loop in some previous life?
the compiler says "expected ';' before '}' token"
tilt_sens_servo_debounce1.cpp: In function 'void loop()':
tilt_sens_servo_debounce1:55: error: expected `;' before '}' token
tilt_sens_servo_debounce1:58: error: expected `;' before '}' token
tilt_sens_servo_debounce1:61: error: expected `;' before '}' token
this is the message at the bottom, it was doing this for my sketch too, is there something wrong with the arduino software?
is there something wrong with the arduino software?
If, by "the Arduino software", you are referring to the IDE and the compiler/linker, then, not there is nothing wrong with the IDE, the compiler, or the linker.
If, by "the Arduino software", you are referring to your sketch, then yes, there is plenty wrong with it. Re-read my previous post.
all i did was copy of the example sketches, the part that is wrong is your the part you wrote, i cant see anything wrong with it, but im not very good at programing, everything except the part you wrote is copied of the examples and put in the right places, thanks for all the help
everything except the part you wrote is copied of the examples and put in the right places
I didn't write any code. I simply rearranged the code you posted, to point out the errors in YOUR code.
If you copied that code from somewhere, it was from a bad example. It looks to me like you copied a for loop, and deleted the for statement, then randomly added curly braces to it.
If you copied that code from an example, please define WHICH example you copied it from.
that's embaresing, sorry
i coppied it from the sweep sketch in the examples menu,it seams to be allmost working, i am now getting this error but its only onthe last line
tilt_sens_servo_debounce1:67: error: expected unqualified-id before '=' token
tilt_sens_servo_debounce1:68: error: expected declaration before '}' token
it says "expected constructor, destructor, or type conversion before '='token" do you know what this means?
do you know what this means?
Yes, but in order to explain it to you, so you know what to fix, and how, you need to post your code.
// constants won't change. They're used here to
// set pin numbers:
#include <Servo.h>
const int buttonPin = 12; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
Servo myservo;
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int pos = 0;
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 200; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(12, HIGH); // pull-up
myservo.attach(9);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}
// set the LED using the state of the button:
digitalWrite(ledPin, buttonState);
if(buttonState==LOW); // The semicolon on the end IS the body of this
// if statement, and is a no-operation statement
{
pos = 0
;pos < 180
;pos += 1
// in steps of 1 degree
;myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
thanks
if(buttonState==LOW); // The semicolon on the end IS the body of this
// if statement, and is a no-operation statement
The example you copied did NOT have a semicolon at the end of the if statement. Why did you put one there?
delay(15); // waits 15ms for the servo to reach the position
}
}
I've suggested that you put the open and close curly braces on separate lines. Use Tools + Autoformat to have the IDE properly indent all your code for you. You will see that the last } above ends loop. The rest of the code is not inside a function where it belongs. The compiler doesn't like that.
{
pos = 0
;pos < 180
;pos += 1
// in steps of 1 degree
;myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Semicolons go at the end of the statement, not on the next line in front of the next statement.
Please explain what you are doing here. This code was NOT in any example provided with the IDE, unless you seriously butchered it. It looks like you removed the for keyword, and are just randomly inserting punctuation to try to make the compiler happy. You should be trying to understand the code, so you know what is missing when the compiler complains.
thanks so much for all you help, ive finally got it to work