hey guys
i'm working on a active spoiler system , so the speed on the servo( spoilers) need to be changing.
so you have a encoder that's your steering wheel, if you move you encoder the servo that needs the move the wheels move's all the time , but then you have your servo for your spoilers that one will only move if the speed of the car is above 70kM/H. i can see that it works fine. ( still servo making a weird sound)
and yes i use a external power supply with micro usb from pc, the man problem is that i want to let them move on a slow speed , when the car is driving slow because there not much of use , but when the car is moving faster so will the spoiler( servo) do aswel (spoeilerstand = spoeilerstand +((kM - servo )*10000) i know this is wrong but i don't know the right combination to make it so it can count up slowly an count down slowly.
sorry for your trouble and thanks in advance
bool membit2 = LOW;
int spoeilerstand = 70;
int teller = 0;
int test = LOW;
float ext = 2.0;
const int interrupt = 2;
const int interrupt2 = 3;
const int interrupt3 = 21;
const int interrupt4 = 20;
bool membit1 = LOW;
int pos = 90;
int switchstate = 0;
const int nachtlichtpin = 4;
bool nachtlicht = LOW;
int valRead = 0;
int val = 0;
int kM = 0;
int snelheid = 0;
int rem = 0;
int RemRead = 0;
int servo = 90;
int stuur = 90;
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 7, d5 = 8, d6 = 6, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
bool display_beschreven = LOW;
#include <Servo.h>
Servo stuur1;
Servo spoiler;
void setup() {
stuur1.attach(9);
spoiler.attach(14);
pinMode(10,OUTPUT);// NACHT LICHTEN
pinMode(nachtlichtpin, INPUT); // NACHT LICHTEN AAN/UIT
pinMode(13, OUTPUT); // REM LICHTEN
digitalWrite(4,LOW);
digitalWrite(10,LOW);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
cli();
TCCR2A = 0;
TCCR2B = 5;
TCNT2 = 0;
OCR2A = 250;
TCCR2A |= (1<<WGM21);
TCCR2B |= (1<<CS21);
TIMSK2 |= (1<<OCIE2A);
sei();
attachInterrupt(digitalPinToInterrupt(interrupt), Interrupttask, RISING);
attachInterrupt(digitalPinToInterrupt(interrupt2), Interrupttask2, RISING);// eerst pin number , void regegister , dan meet moment
attachInterrupt(digitalPinToInterrupt(interrupt3), Interrupttask3, CHANGE);
attachInterrupt(digitalPinToInterrupt(interrupt4), Interrupttask4, CHANGE);
Serial.begin(9600);
}
void loop(){
digitalWrite(nachtlichtpin, nachtlicht);
if (nachtlicht == HIGH){
digitalWrite(10,HIGH);
} else{
digitalWrite(10,LOW);
}
if (RemRead > 3){
digitalWrite(13,HIGH);
}
else{
digitalWrite(13,LOW);
}
if (membit1 == HIGH){
switchstate = digitalRead(nachtlichtpin);
if ((switchstate == HIGH) && (test == LOW)){
nachtlicht = !nachtlicht;
test = HIGH;
}
if (( switchstate == digitalRead(nachtlichtpin)) && (switchstate == LOW)){
test = LOW;
}
lcd.begin(20,4);
kM = snelheid/100;
if (kM > 240){
kM = 240;}
lcd.print(kM);
lcd.setCursor(3,0);
lcd.print("km/h");
delay(20);
Serial.print(spoeilerstand); // prints a label
Serial.print("ervo");
if (kM > 70){
spoeilerstand = spoeilerstand +((kM - servo )*10000);
} else {
spoeilerstand = 90;
}
spoiler.write (constrain (map (spoeilerstand, -10000, 10000, 0, 180), 0, 180));
stuur1.write (constrain (map (servo, -10000, 10000, 0, 180), 0, 180));
if (membit2 = 1){
rem = analogRead(A2);
val = analogRead(A1);
valRead = map(val, 0, 1023, 0, 240);
RemRead = map(rem, 0, 1023, 0 , 50);
snelheid = snelheid + valRead/10 - snelheid/2000 - RemRead;
if(snelheid <0){ snelheid = 0;
}
if (snelheid > 24000){
snelheid = 24000;
}
}
membit2 = LOW;
display_beschreven = LOW;
}
else{
if (display_beschreven == LOW) {
lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print("PRESS START");
delay(20);
display_beschreven = HIGH;
}
}
}
void Interrupttask() {
membit1 = HIGH;}
void Interrupttask2() {
membit1 = LOW;
}
ISR(TIMER2_COMPA_vect){
// membit2 = HIGH;
rem = analogRead(A2);
val = analogRead(A1);
valRead = map(val, 0, 1023, 0, 240);
RemRead = map(rem, 0, 1023, 0 , 50);
snelheid = snelheid + valRead/10 - snelheid/2000 - RemRead;
if(snelheid <0){ snelheid = 0;
}
if (snelheid > 24000){
snelheid = 24000;
}}
void Interrupttask3(){
if (digitalRead(interrupt3) == HIGH ) {
if ( digitalRead(interrupt4) == HIGH){
servo--;
}
else {
servo++;
}
}else{
if ( digitalRead(interrupt4) == HIGH){
servo++;
}
else {
servo--;
}
}
}
void Interrupttask4(){
if (digitalRead(interrupt3) == HIGH ) {
if ( digitalRead(interrupt4) == HIGH){
servo++;
}
else {
servo--;
}
}else{
if ( digitalRead(interrupt4) == HIGH){
servo--;
}
else {
servo++;
}
}
}