Hi there,
I am trying really hard to make my 2 sketches combined with eachother so it runs togetcher.
I have tried to work with the code millis, and it just doest work, and don't really get the programming right.
Can someone please look at my sketches and tell me how i properly can combine them, so it runs both?
Thanks and love
Here is the Servo sketch
#include <Servo.h>
long randomNumber;
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(9);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
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(1000);
}
this is the button with Led on off sketch
int pinButton = 8;
int LED = 2;
int stateLED = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 200;
void setup() {
pinMode(pinButton, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
if(stateLED == HIGH){
stateLED = LOW;
} else {
stateLED = HIGH;
}
time = millis();
}
digitalWrite(LED, stateLED);
previous == stateButton;
I really hope someone can help me.
Please post your combined sketch and explain what it should do and what it actually does.
Hi,
I want the servo move randomly (this sketch works on it's own)
I also want to have a button and a LED pin switching on and off when i press it (this also works on its own)
When i combine the 2 sketches ( look below for sketch) the servo does move randomly, but the
LED onlly turn on and of if i hold the button. So when the servo makes a sweep, the signal of the button gives a signal to the LED and it turns on and of with every sweep(when button is pressed).
I want to make the servo make random sweeps, and in every delay i want to push the button the lights goes on and off. without interupting the delay. After the button is pressed i want the random sweeps going on.
Thanks
int pinButton = 8;
int LED = 2;
int stateLED = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 200;
#include <Servo.h>
long randomNumber;
Servo myservo;
int pos = 0;
void setup() {
{
pinMode(pinButton, INPUT);
pinMode(LED, OUTPUT);
}
{
myservo.attach(9);
Serial.begin(9600);
randomSeed(analogRead(0));
}
}
void loop(){
{
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(1000);
}
stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
if(stateLED == HIGH){
stateLED = LOW;
} else {
stateLED = HIGH;
}
time = millis();
}
digitalWrite(LED, stateLED);
previous == stateButton;
}
When i combine the 2 sketches ( look below for sketch) the servo does move randomly,
No, there is no way the servo moves with that code you have posted.
The servo will only move when you command it to move and there is no statement to do that in your code. You do have such a statement in that code but it is surrounded by a /* and */ which means the code is turned into a comment and so never gets compiled.
Please copy and paste code from the IDE do not type it in.
Doing it the easy way compiles without error. What 'doesn't work' about it?
void setup()
{
setup1();
setup2();
}
void loop()
{
loop1();
loop2();
}
#include <Servo.h>
long randomNumber;
Servo myservo;
int pos = 0;
void setup1()
{
myservo.attach(9);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop1()
{
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(1000);
}
int pinButton = 8;
int LED = 2;
int stateLED = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 200;
void setup2()
{
pinMode(pinButton, INPUT);
pinMode(LED, OUTPUT);
}
void loop2()
{
stateButton = digitalRead(pinButton);
if (stateButton == HIGH && previous == LOW && millis() - time > debounce)
{
if (stateLED == HIGH)
{
stateLED = LOW;
}
else
{
stateLED = HIGH;
}
time = millis();
}
digitalWrite(LED, stateLED);
previous == stateButton;
}
Note: That last line should probably be "previous = stateButton;" because doing a comparison and not looking at the result tends to have no effect.