Hallo zusammen,
ich habe mir ein Modell Fahrlader gebaut, dieser wird aktuell mit einer Arduino mega betrieben.
Eingabe erfolgt über einen Bluetooth-Controller (PS2) damit werden mehrere Zylinder (Motoren) und LED´s angesteuert. Das Programm ist immer weitergewachsen und habe nach und nach Beleuchtung hinzugefügt.
Nun habe ich alles angeschlossen und alle Funktionen sind geben, lediglich die Bewegung bzw. die Motoren laufen nicht sauber durch. Meine Vermutung ist, dass die einzelnen Delay´s für die Selbst Haltung (Tasten entprellen) der Beleuchtung dieses verursachen.
Wie kann ich dieses nun beheben? Die Bewegungen sollen schon flüssig durchlaufen. Wie kann dieses am besten umgesetzt werden? Hatte leider kein Beispiel oder ein ähnliches Projekt gefunden.
Danke und Gruß
Alex
#include <PS2X_lib.h> //for v1.6
#include <Servo.h>
Servo servo1;
/******************************************************************
set pins connected to PS2 controller:
- 1e column: original
- 2e colmun: Stef?
replace pin numbers by the ones you use
******************************************************************/
#define PS2_DAT 6 //14 ((13))
#define PS2_CMD 7 //15 ((11))
#define PS2_SEL 8 //16 ((10))
#define PS2_CLK 9 //17 ((12))
/******************************************************************
select modes of PS2 controller:
- pressures = analog reading of push-butttons
- rumble = motor rumbling
uncomment 1 of the lines for each mode selection
******************************************************************/
//#define pressures true
#define pressures false
//#define rumble true
#define rumble false
//#define pin7 7
//#define pin6 6
PS2X ps2x; // create PS2 Controller Class
//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you connect the controller,
//or call config_gamepad(pins) again after connecting the controller.
int error = 0;
byte type = 0;
byte vibrate = 0;
int pin6 = 3; // Kippzylinder pwm
int pin7 = 2; //Kippzylinder digital
int pin5 = 12; //hubzylinder pwm
int pin4 = 13; //hubzylinder digital
int pin8 = 5; //fahren pwm
int pin9 = 4; //fahren digital
int val1;// bucket up
int val2; //bucket down
int val3; //Liftarm
int val4; //Liftarm
int val5; // vorwärts
int val6; // rückwarts
int val7;// lenken links
int val8; // lenken rechts
///////////////ENDschalter
int s0 = A0;//kippzylinder oben
int s1 = A1;//kippzylinder unten
int s2 = A2;//hubzylinder unten
int s3 = A3;//hubzylinder oben
int stepdelay;
//////////////////////Beleuchtung Abblendlicht//////////////////
const int ledPin1 = 16 ; // Abblendlicht vorne
const int ledPin3 = 19; // Abblendlicht hinten
int buttonPushCounter = 0;
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
/////////////////////////Beleuchtung Fernlicht////////////////
const int ledPin2 = 17; // Fernlicht vorne
const int ledPin4 = 18; // Fernlicht hinten
int buttonPushCounter2 = 0;
int buttonState2 = 0; // current state of the button
int lastButtonState2 = 0; // previous state of the button
/////////////////////////Beleuchtung Dreick Stoßbeleuchtung ////////////////
const int ledPin5 = 14; // stoßbeleuchtung
int buttonPushCounter3 = 0;
int buttonState3 = 0; // current state of the button
int lastButtonState3 = 0; // previous state of the button
/////////////////////////Beleuchtung Viereck RUK ////////////////
const int ledPin6 = 15; // RUK
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
int buttonPushCounter4 = 0;
int buttonState4 = 0; // current state of the button
int lastButtonState4 = 0; // previous state of the button
////////////////// Position leuchte /////////////////////////7
const int ledPin9 = 20; // RUK
void setup() {
Serial.begin(57600);
delay(300); //added delay to give wireless ps2 module some time to startup, before configuring it
pinMode(pin6, OUTPUT); // bucket pin6
pinMode(pin7, OUTPUT); // bucket pin7
pinMode(pin5, OUTPUT); // liftarm Pin 5
pinMode(pin4, OUTPUT); // liftarm pin 1
pinMode(pin8, OUTPUT); // fahren pwm
pinMode(pin9, OUTPUT); // fahren digital
pinMode(ledPin1, OUTPUT);//LED 1
pinMode(ledPin2, OUTPUT);//LED 2
pinMode(ledPin3, OUTPUT);//LED 3
pinMode(ledPin4, OUTPUT);//LED 4
pinMode(ledPin5, OUTPUT);//LED 5
pinMode(ledPin6, OUTPUT);//LED 6
//pinMode(ledPin7, OUTPUT);//LED 7
pinMode(A0, INPUT);//kippzylinder unten
pinMode(A1, INPUT);//kippzylinder oben
pinMode(A2, INPUT);//hubzylinder unten
pinMode(A3, INPUT);//hubzylinder oben
servo1.attach(10); //PIn 10
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if (error == 0) {
digitalWrite (ledPin9, HIGH);
Serial.print("Found Controller, configured successful ");
Serial.print("pressures = ");
if (pressures)
Serial.println("true ");
else
Serial.println("false");
Serial.print("rumble = ");
if (rumble)
Serial.println("true)");
else
Serial.println("false");
//Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
//Serial.println("holding L1 or R1 will print out the analog stick values.");
//Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
}
else if (error == 1)
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
else if (error == 2)
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
else if (error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
// Serial.print(ps2x.Analog(1), HEX);
type = ps2x.readType();
switch (type) {
case 0:
Serial.print("Unknown Controller type found ");
break;
case 1:
Serial.print("DualShock Controller found ");
break;
case 2:
Serial.print("GuitarHero Controller found ");
break;
case 3:
Serial.print("Wireless Sony DualShock Controller found ");
break;
}
}
void loop() {
/* You must Read Gamepad to get new values and set vibration values
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
You should call this at least once a second
*/
if (error == 1) //skip loop if no controller found
return;
if (type == 2) { //Guitar Hero Controller
ps2x.read_gamepad(); //read controller
if (ps2x.ButtonPressed(STAR_POWER))
Serial.println("Star Power Command");
if (ps2x.Button(UP_STRUM)) //will be TRUE as long as button is pressed
Serial.println("Up Strum");
if (ps2x.Button(DOWN_STRUM))
Serial.println("DOWN Strum");
if (ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
Serial.println("Start is being held");
if (ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");
if (ps2x.Button(ORANGE_FRET)) { // print stick value IF TRUE
Serial.print("Wammy Bar Position:");
Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
}
}
else { //DualShock Controller dieser Teile wird verwedet //////////////////////
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
if (ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
Serial.println("Start is being held");
if (ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");
if (ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
Serial.print("Up held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
}
if (ps2x.Button(PSB_PAD_RIGHT)) {
Serial.print("Right held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
}
if (ps2x.Button(PSB_PAD_LEFT)) {
Serial.print("LEFT held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
}
if (ps2x.Button(PSB_PAD_DOWN)) {
Serial.print("DOWN held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
}
vibrate = ps2x.Analog(PSAB_CROSS); //this will set the large motor vibrate speed based on how hard you press the blue (X) button
if (ps2x.NewButtonState()) { //will be TRUE if any button changes state (on to off, or off to on)
if (ps2x.Button(PSB_L3))
Serial.println("L3 pressed");
if (ps2x.Button(PSB_R3))
Serial.println("R3 pressed");
if (ps2x.Button(PSB_L2))
Serial.println("L2 pressed");
if (ps2x.Button(PSB_R2))
Serial.println("R2 pressed");
if (ps2x.Button(PSB_TRIANGLE))
Serial.println("Triangle pressed");
}
if (ps2x.ButtonPressed(PSB_CIRCLE)) //will be TRUE if button was JUST pressed
Serial.println("Circle just pressed");
if (ps2x.NewButtonState(PSB_CROSS)) //will be TRUE if button was JUST pressed OR released
Serial.println("X just changed");
if (ps2x.ButtonReleased(PSB_SQUARE)) //will be TRUE if button was JUST released
Serial.println("Square just released");
//print stick values if either is TRUE
// Serial.print("Stick Values:");
// Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
// Serial.print(",");
// Serial.print(ps2x.Analog(PSS_LX), DEC);
// Serial.print(",");
Serial.print(ps2x.Analog(PSS_RY), DEC);
Serial.print(",");
Serial.println(ps2x.Analog(PSS_RX), DEC);
delay(50);
}
int nJoyX = ps2x.Analog(PSS_LX); // read x-joystick rechts
int nJoyY = ps2x.Analog(PSS_LY); // read y-joystick rechts
nJoyX = map(nJoyX, 0, 255, -1023, 1023);
nJoyY = map(nJoyY, 0, 255, 1023, -1023);
int nJoyXl = ps2x.Analog(PSS_RX); // read x-joystick links
int nJoyYl = ps2x.Analog(PSS_RY); // read y-joystick links
nJoyXl = map(nJoyXl, 0, 255, -1023, 1023);
nJoyYl = map(nJoyYl, 0, 255, 1023, -1023);
/////// vorwärts fahren ////////////////////////////////////////////7
if (nJoyY >= 40) {
Serial.print("forward");
val5 = ps2x.Analog(PSS_RY);
val5 = map (val5, 127 , 0 , 0, 255);
{
digitalWrite (pin8, val5);
Serial.print("val5:"); Serial.print(val5); Serial.print(pin6);
digitalWrite (pin9, HIGH);
Serial.print("HIGH");
delayMicroseconds (100);
}
}
else if (nJoyY <= 40) {
analogWrite (pin8, 0);
}
////////// rückfährts/////////////////////////
if (nJoyY <= -550) {
Serial.print("backward");
val6 = ps2x.Analog(PSS_RY);
val6 = map (val6, 127, 256 , 100, 255);
{
analogWrite (pin8, val6);
Serial.print("val6:"); Serial.print(val6);
digitalWrite (pin9, LOW);
digitalWrite(ledPin2, LOW);
delayMicroseconds (100);
}
}
else if (nJoyY <= -400) {
analogWrite (pin8, 0);
delayMicroseconds (100);
}
///////////////////lenken rechts ////////////////////7
if (nJoyX >=60) {
Serial.print("lenken rechts");
val8 = ps2x.Analog(PSS_LX);
val8 = map (val8, 160, 254 , 90, 30);
servo1.write(val8);
Serial.print("val8:"); Serial.print(val8);
delayMicroseconds(1000);
}
//////////////////LEnkeb links///////////////////////
if (nJoyX <= -550) {
Serial.print("lenken links");
val7 = ps2x.Analog(PSS_LX);
val7 = map (val7, 100, 0 , 90, 150);
servo1.write(val7);
Serial.print("val7:"); Serial.print(val7);
delayMicroseconds(1000);
}
// bucket down Motor 2 rechtsrum
if ((nJoyYl >=100) && (digitalRead(A0) ==LOW)) {
Serial.print("bucket down");
val2 = ps2x.Analog(PSS_RY);
val2 = map (val2, 127 , 0 , 0, 255);
{
digitalWrite (pin6, val2);
Serial.print("val2:"); Serial.print(val2);Serial.print(pin6);
digitalWrite (pin7, HIGH);
Serial.print("HIGH");
delayMicroseconds (100);
}
}
else if ((nJoyYl <= 40)|(digitalRead(A0) ==HIGH)) {
analogWrite (pin6, 0);
}
// bucket up Motor 2 linksrum
if ((nJoyYl <= -550)&& (digitalRead(A1) ==LOW)) {
Serial.print("bucket up");
val1 = ps2x.Analog(PSS_RY);
val1 = map (val1, 127, 256 , 100, 255);
{
analogWrite (pin6, val1);
Serial.print("val1:"); Serial.print(val1);
digitalWrite (pin7, LOW);
delayMicroseconds (100);
}
}
else if ((nJoyYl <= -400)| ((nJoyYl <= -400)&&(digitalRead(A1) ==HIGH))) {
analogWrite (pin6, 0);
delayMicroseconds (100);
}
// lirmarm down
if ((nJoyXl <= -550)&&(digitalRead(A2) ==LOW)) {
Serial.print("lirmarm down");
val4 = ps2x.Analog(PSS_RX);
val4 = map (val4, 127 , 0 , 0, 255);
{
analogWrite(pin5, val4); //geschwindigkeit
Serial.print("val4:"); Serial.print(val4);
digitalWrite (pin4, LOW); //drehrichtung
delayMicroseconds(100);
}
}
else if ((nJoyXl >= -500)| ((nJoyXl <= -400)&&(digitalRead(A2) ==HIGH))) {
analogWrite(pin5, 0);
}
// lirmarm up Motor 1 rechtsrum
if ((nJoyXl >=60)&&(digitalRead(A3) ==LOW)) {
Serial.print("lirmarm up");
val3 = ps2x.Analog(PSS_RX);
val3 = map (val3, 127, 256 , 100, 255);
{
analogWrite(pin5, val3); //geschwindigkeit
Serial.print("val3:"); Serial.print(val3);
digitalWrite (pin4, HIGH); //drehrichtung
delayMicroseconds(100);
}
}
else if ((nJoyXl < 40)|(digitalRead(A3) ==HIGH)) {
digitalWrite (pin4, LOW);
}
if (digitalRead(A2) ==HIGH)
{ Serial.print("s0 high");
}
else if (digitalRead(A0) ==LOW)
{Serial.print("s0 low");
}
if (digitalRead(A3) ==HIGH)
{ Serial.print("s1 high");
}
else if (digitalRead(A1) ==LOW)
{Serial.print("s1 low");
}
////////////////Beleuchtung///////////////
// LED X Abblendlicht /////////////////////////////////////////////////////////
buttonState = (ps2x.NewButtonState(PSB_CROSS));
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(10);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin1, HIGH); //Abblendlicht vorne
digitalWrite(ledPin3, HIGH); //Abblendlicht hinten
Serial.println("ledPin1, HIGH");
}
else {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin3, LOW);
Serial.println("ledPin, LOW");
}
///////////////LED 2 Kreis Fernlicht////////////////
buttonState2 = (ps2x.NewButtonState(PSB_CIRCLE));
if (buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState2 == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter2++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter2);
}
else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(5);
}
// save the current state as the last state, for next time through the loop
lastButtonState2 = buttonState2;
if (buttonPushCounter2 % 4 == 0) {
digitalWrite(ledPin2, HIGH); //Fernlicht vorne
digitalWrite(ledPin4, HIGH); //Fernlicht hinten
Serial.println("ledPin2, HIGH");
}
else {
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin4, LOW);
Serial.println("ledPin2, LOW");
}
///////////////LED Dreieck Stoßbleuchtung ////////////////
buttonState3 = (ps2x.NewButtonState(PSB_TRIANGLE));
if (buttonState3 != lastButtonState3) {
// if the state has changed, increment the counter
if (buttonState3 == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter3++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter3);
}
else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(10);
}
// save the current state as the last state, for next time through the loop
lastButtonState3 = buttonState3;
if (buttonPushCounter3 % 4 == 0) {
digitalWrite(ledPin5, HIGH);
Serial.println("ledPin5, HIGH");
}
else {
digitalWrite(ledPin5, LOW);
Serial.println("ledPin5, LOW");
}
///////////////LED Viereck RUK ////////////////
unsigned long currentMillis = millis();
buttonState4 = (ps2x.NewButtonState(PSB_SQUARE));
if (buttonState4 != lastButtonState4) {
// if the state has changed, increment the counter
if (buttonState4 == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter4++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter4);
}
else {
}
// Delay a little bit to avoid bouncing
delay(10);
}
// save the current state as the last state, for next time through the loop
lastButtonState4 = buttonState4;
if (buttonPushCounter4 % 4 == 0) {
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
}
else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin6, ledState);
}
}
}