Oh zo dat is voor mij te ingewikkeld denk ik haha bedankt voor de snelle reactie
Valt wel mee ben, wel even bezig geweest met het programmeren is nog niet helemaal klaar wacht nog op de tweede motor driver voor de kop en heckschroef en wat meer spullen servos voor de kranen enz
maar zal laten zien wat ik tot nu toe heb geprogrammeerd zal niet perfect zijn is ook de eerste keer dat ik met een arduino werk belangrijkste is het werkt allemaal wel
#include <Servo.h>
const int SERIAL_BUFFER_SIZE = 8;
char serialBuffer[SERIAL_BUFFER_SIZE];
boolean newData = false;
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
//servo
const int servo1 = 32; // Pin for the first servo
int servoVal;
Servo myservo1; // create servo object to control a servo
void Motor_1();
void bluethooth();
void recvWithEndMarker();
void showNewData();
//l298n -------------
// Motor A
int enA =2;
int in1 = 52;
int in2 = 50;
// Motor B
int enB = 3;
int in3 = 51;
int in4 = 53;
//Motor Speed Values - Start at zero
int MotorSpeed1 = 0;
int MotorSpeed2 = 0;
//rc kanalen---------
double channel[2];
const int pwmPIN[]={4,5,6,7,8,11,12,13}; // an array to identify the PWM input pins (the array can be any length)
int RC_inputs = 0;
int i;
int buttonState =0;
int lastbuttonState = 0;
int x = 0;
void setup(){
myservo1.attach(servo1); // attaches the servo
for(i=22;i<30;i++)
{
pinMode(i, OUTPUT);//relay pins
digitalWrite(i, HIGH);
}
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
int motorA=0;
int motorB = 0;
// Start with motors disabled and direction forward
// Motor A
digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Motor B
digitalWrite(enB, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.begin(9600);
}
void loop() {
channel[0]=pulseIn(7, HIGH,50000);//ch2 ln298 rechter joystick x as
channel[1]=pulseIn(8, HIGH,50000);//ch4 ln298 rechter joystick y as
channel[3]=pulseIn(6, HIGH,50000);//ch8 ln298
channel[4]=pulseIn(4, HIGH,50000);//ch 14 relais1 en relais 2
channel[5]=pulseIn(5, HIGH,50000);
// failsave voor als het rc signaal wegvalt stopt het script
//failsave
int sIn = 4;
unsigned long dur;
unsigned long failsafeVal = 1900;
int debounce = 1; //seconds//amount of time to regain rc signal from the time its lost
unsigned long lastDisconnection = 0;
unsigned long previousMillis = 0;
int RCstatus = 0;
int prevStatus = 0;
unsigned long currentMillis = millis();
dur = pulseIn(sIn, HIGH, 500000);
//falls to acceptable range
if (dur >= 950 && dur <= 2000)
{
//check if it regains signal
if (prevStatus == 2 && (currentMillis - lastDisconnection) < (debounce * 1000))
dur = failsafeVal;
Serial.print(dur);
Serial.print("-");
Serial.println (currentMillis);
Motor_1();
} else {
bluethooth();
}
}
void Motor_1(){
//y as biede motoren syncroon voor achteruit x as alleen linker of rechter motor met roerservo-----------
if (channel[3] >1500){
if (channel[0] < 1400)
{
// Set Motor A backward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B backward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
MotorSpeed1 = map(channel[0], 1400, 1020, 0, 255);
MotorSpeed2 = map(channel[0], 1400,1020, 0,255);
}
else if (channel[0] > 1500)
{
// This is Forward
// Set Motor A forward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B forward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
//Determine Motor Speeds
MotorSpeed1 = map(channel[0],1480, 1930, 0, 255);
MotorSpeed2 = map(channel[0],1480, 1930, 0, 255);
}
else
{
// This is Stopped
MotorSpeed1 = 0;
MotorSpeed2 = 0;
}
// Now do the steering
if (channel[1] < 1400)
{
// Move Left
i = map(channel[1], 1400, 1020, 0, 255);
MotorSpeed1 = MotorSpeed1 - i;
MotorSpeed2 = MotorSpeed2 + i;
if (MotorSpeed1 < 0)MotorSpeed1 = 0;
if (MotorSpeed2 > 255)MotorSpeed2 = 255;
}
else if (channel[1] > 1550)
{
// Move Right
i= map(channel[1], 1508, 1930, 0, 255);
MotorSpeed1 = MotorSpeed1 + i;
MotorSpeed2 = MotorSpeed2 -i;
// Don't exceed range of 0-255 for motor speeds
if (MotorSpeed1 < 0)MotorSpeed1 = 0;
if (MotorSpeed2 > 255)MotorSpeed2 = 255;
}
}
//alleen syncroon draaien met roerservo -------yas motoren x as roerservo---------------
if ( channel[3]>1400 && channel[3]<1600){
if (channel[0] < 1400)
{
// Set Motor A backward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B backward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
MotorSpeed1 = map(channel[0], 1400, 1010, 0, 255);
MotorSpeed2 = map(channel[0], 1400,1010, 0,255);
}
else if (channel[0] > 1500)
{
// This is Forward
// Set Motor A forward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B forward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
//Determine Motor Speeds
MotorSpeed1 = map(channel[0],1500, 1930, 0, 255);
MotorSpeed2 = map(channel[0],1500, 1930, 0, 255);
}
else
{
// This is Stopped
MotorSpeed1 = 0;
MotorSpeed2 = 0;
}
}
if ( channel[3]<1400){
if (channel[1]>1500){
//Motor A achteruit
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B vooruit
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
MotorSpeed1 = map(channel[1],1500, 1930, 0, 255);
MotorSpeed2 = map(channel[1],1500, 1930, 0, 255);
}
else if ( channel[3]<1400){
if ( channel[1]<1500){
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
MotorSpeed1 = map(channel[1], 1400, 1020, 0, 255);
MotorSpeed2 = map(channel[1], 1400,1020, 0,255);
}
}else {
// This is Stopped
MotorSpeed1 = 0;
MotorSpeed2 = 0;
}
}
if (MotorSpeed1 < 30)MotorSpeed1 = 0; // Adjust to prevent "buzzing" at very low speed
if (MotorSpeed2 < 30)MotorSpeed2 = 0;
analogWrite(enA, MotorSpeed1); // Set the motor speeds
analogWrite(enB, MotorSpeed2);
// servo roer
// servo roer
if (channel[1]>1490){//sturen bij syncroon draaien
servoVal= map(channel[1], 1485, 1915,90,0); // sturen met beide motoren knop middenstand
}else if (channel[1]<1450){
servoVal= map(channel[1], 1480,1020, 90, 180);
}else if (channel[1]>1400 && channel[1]<1550){
servoVal = 90;
}
myservo1.write(servoVal);
delay(5);
if(channel[4]>1600 ){
buttonState =digitalRead (22);
if (buttonState ==HIGH){
digitalWrite (22, LOW);
Serial.print (channel[4]);
} else if(channel[4]>1600 ){
digitalWrite (22, HIGH);
}
delay(300);
}
if(channel[4]<1200 && channel[4]>800 ){
buttonState =digitalRead (23);
if (buttonState ==HIGH){
digitalWrite (23, LOW);
} else if(channel[4]<1200 && channel[4]>800){
digitalWrite (23, HIGH);
}
delay(300);
}
if (channel[5] >1600){
digitalWrite (24, LOW);
}
else if(channel[5]<1200)
{
digitalWrite(25,LOW);
}
else
{
digitalWrite (24, HIGH);
digitalWrite (25,HIGH);
}
}
void bluethooth(){
static byte ndx = 0;
char startmarker;
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
} else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
int motorA;
int motorB ;
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Start with motors disabled and direction forward
// Motor A
digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Motor B
digitalWrite(enB, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
int angle=0;
if (newData == true) {
char input=receivedChars;
Serial.print(receivedChars);
Serial.print("-");
int Blt = atol(receivedChars);
Serial.println(Blt);
// if startmarker=C
switch (Blt) {
case 1: // Dekverlichting 1
digitalWrite(22, LOW);
break;
case 2:
digitalWrite(22,HIGH);
break;
case 3: //Dekverlichting2
digitalWrite(23,LOW);
break;
case 4: //
digitalWrite(23,HIGH);
break;
case 5: //Stuurhutlicht
digitalWrite(24, LOW);
break;
case 6:
digitalWrite(24,HIGH);
break;
case 7: //schijnwerpers
digitalWrite(25,LOW);
break;
case 8: //
digitalWrite(25,HIGH);
break;
case 9: /navigatielichten
digitalWrite(26, LOW);
break;
case 10:
digitalWrite(26,HIGH);
break;
case 11: //sleeplichten
digitalWrite(27,LOW);
break;
case 12:
digitalWrite(27,HIGH);
break;
case 13: //radars
digitalWrite(28, LOW);
break;
case 14:
digitalWrite(28,HIGH);
break;
case 15:
digitalWrite(29,LOW);
break;
case 16: //
digitalWrite(29,HIGH);
break;
case 109: //Stuur BB
servoVal = 178;
myservo1.write(servoVal);
break;
case 110:
servoVal = 90;
myservo1.write(servoVal);
break;
case 111: //Stuur SB
servoVal = 0;
myservo1.write(servoVal);
break;
case 112:
servoVal = 90;
myservo1.write(servoVal);
break;
case 100: ///motor A vooruit
motorA=2;
break;
case 102: ///motor A stop
motorA = 0;
break;
case 113: ///motor A stopknop
motorA = 0;
break;
case 105: // // set motorA achteruit
motorA= 1;
break;
case 106: //// zet motorA stop
motorA = 0;
break;
case 107: //: // // set motorB achteruit
motorB = 1;
break;
case 108: //// zet motorB stop
motorB = 0;
break;
case 114: //// zet motorB stopknop
motorB = 0;
break;
case 103: //// set motorB voouit
motorB =2;
break;
case 104: //// zet motorB stop
motorB = 0;
break;
}
if (motorA == 1 && Blt==406){
// Set Motor A backward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
MotorSpeed1 = 40; ;
Serial.print("A achteruit");
}else if (motorA == 2 && Blt == 404){
// Set Motor A forward
Serial.print(" A vooruit");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
MotorSpeed1 = 40;
}else if (motorA == 0){
Serial.print(" A stop");
MotorSpeed1 = 0;
}
if (motorB == 1 &&Blt ==408){
// Set Motor B backward
Serial.print("B achteruit");
digitalWrite(enB, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
MotorSpeed2 = 40;
}else if (motorB == 2 &&Blt==410){
// Set Motor B forward
Serial.print(" B vooruit");
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
MotorSpeed2 =40;
}else if(motorB == 0){
Serial.print(" b stop");
MotorSpeed2 = 0;
}
if (Blt > 0 && Blt < 256) {
if ( motorA== 1 || motorA == 2){
MotorSpeed1= map (Blt,1000,1255,30,255);
}
}
if ( Blt> 600 && Blt< 855 ){
if ( motorB == 1 || motorB== 2){
MotorSpeed2 = map (Blt,2000,2255,30,255);
}
}
if ( Blt> 3000&& Blt< 3255 ){
if ( motorB == 1 && motorA== 1|| motorB== 2 && motorA==2 || motorA==1 && motorB == 2 || motorA==2 && motorB ==1){
MotorSpeed1 = map (Blt,3000,3255,30,255);
MotorSpeed2 = map (Blt,3000,3255,30,255);
}
}
Serial.print("-A=");
Serial.print(motorA);
Serial.print("-B=");
Serial.print(motorB);
Serial.print("-ms1_");
Serial.print (MotorSpeed1);
Serial.print("-ms2_");
Serial.println (MotorSpeed2);
analogWrite(enA, MotorSpeed1); // Set the motor speeds
analogWrite(enB, MotorSpeed2);
}
newData = false;
}
}