I just made one of my first sketches and have problems with the breadboard.
I have everything hooked up with the board and have a sketch where i want to make the servo move randomly independenty, not interacting with the pushbutton and LED.
The pushbutton and LED i want to turn of with one push and turn on with one push.
Could somebody look to my sketch and see the problem?
Thanks a lot.
#include <Servo.h>
long randomNumber;
Servo myservo;
int pos = 0;
int buttonPin = 7;
int ledPin = 8;
boolean on = false;
int buttonstate = 0;
void setup() {
myservo.attach(9);
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonstate = digitalRead(buttonPin);
if (buttonstate == HIGH) {
if (on == true) {
on = false;
} else {
on = true;
}
}
if (on == true) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
delay(5);
{
randomNumber = random(10000);
Serial.print("random:");
Serial.println(randomNumber);
randomNumber = map(randomNumber, 0, 10000, 0, 180);
Serial.print("servo:");
Serial.println(randomNumber);
myservo.write(randomNumber);
/*
for (pos = 0; pos <= 180; pos += 1)
myservo.write(pos);
delay(100);
for (pos = 180; pos >= 0; pos -= 1)
myservo.write(pos);
delay(1000);
*/
delay(10000);
}
}
I just made one of my first sketches and have problems with the breadboard.
I have everything hooked up with the board and have a sketch where i want to make the servo move randomly independenty, not interacting with the pushbutton and LED.
The pushbutton and LED i want to turn of with one push and turn on with one push.
Could somebody look to my sketch and see the problem?
Thanks a lot.
#include <Servo.h>
long randomNumber;
Servo myservo;
int pos = 0;
int buttonPin = 7;
int ledPin = 8;
boolean on = false;
int buttonstate = 0;
void setup() {
myservo.attach(9);
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonstate = digitalRead(buttonPin);
if (buttonstate == HIGH) {
if (on == true) {
on = false;
} else {
on = true;
}
}
if (on == true) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
delay(5);
{
randomNumber = random(10000);
Serial.print("random:");
Serial.println(randomNumber);
randomNumber = map(randomNumber, 0, 10000, 0, 180);
Serial.print("servo:");
Serial.println(randomNumber);
myservo.write(randomNumber);
/*
for (pos = 0; pos <= 180; pos += 1)
myservo.write(pos);
delay(100);
for (pos = 180; pos >= 0; pos -= 1)
myservo.write(pos);
delay(1000);
*/
delay(10000);
}
}
In order to turn on the LED, you have to use digitalWrite( ledPin, HIGH).
I don't see that anywhere in your code. It should come after your logic for setting the value of the 'on' variable
Also, how is your button wired up? If it is connected to ground on one pin and the Arduino on the other pin, you have to enable the internal pullup resistor unless you have an external one.
When you encounter an error you'll see a button on the right side of the orange bar "Copy error messages". Click that button. Paste the error in a message here using code tags.
There's your problem. Look at millis for timing, there's countless examples online, even the first sticky post on this subforum is dedicated to this exact topic. Please read it.
blh64:
In order to turn on the LED, you have to use digitalWrite( ledPin, HIGH).
I don't see that anywhere in your code. It should come after your logic for setting the value of the 'on' variable
Also, how is your button wired up? If it is connected to ground on one pin and the Arduino on the other pin, you have to enable the internal pullup resistor unless you have an external one.
I think i do have digitalWrite( ledPin, HIGH) in my code? Or else, i dont know where to put it if i did it wrong.
My button is connected with a 10k resistor to the arduino and the other pin to the button.
If i put the delay on the bottom of the sketch on a low number, like 100, The servo goes fast and if i push the button the led goes on and of, but i think it has something to do with signals going thru the arduino. I dont really know.
#include <Servo.h>
long randomNumber;
Servo myservo;
int pos = 0;
int buttonPin = 7;
int ledPin = 8;
boolean on = false;
int buttonstate = 0;
void setup() {
myservo.attach(9);
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonstate = digitalRead(buttonPin);
if (buttonstate == HIGH) {
if (on == true) {
on = false;
} else {
on = true;
}
}
if (on == true) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
delay(20);
{
randomNumber = random(10000);
Serial.print("random:");
Serial.println(randomNumber);
randomNumber = map(randomNumber, 0, 10000, 0, 180);
Serial.print("servo:");
Serial.println(randomNumber);
myservo.write(randomNumber);
/*
for (pos = 0; pos <= 180; pos += 1)
myservo.write(pos);
delay(1000);
for (pos = 180; pos >= 0; pos -= 1)
myservo.write(pos);
delay(1000);
*/
delay(100);
}
}
PieterP:
There's your problem. Look at millis for timing, there's countless examples online, even the first sticky post on this subforum is dedicated to this exact topic. Please read it.
Pieter
Hi Pieter
The problem is not that the delay is to high, this delay is connected to the servo and i moves at this speed, wich is what i want.
But now i want to be able to push the button while the servo is moving.
Any ideas about it?
You can't. Because the delay(x) causes the entire processor to just hang for x milliseconds. It cannot do anything else during that time, not even reading buttons*.
That's why you have to use millis for timing.
Pieter
(*) Unless you'd use interrupts, but that's a whole other can of worms.