Hallo miteinander,
ich versuche per Bluetooth 3 Motoren und eine LED zu steuern.
Meine Hardware:
- Arduino Uno V3
- Adafruit Motor Shield V2
- Bluetooth Modul HC-06
- Motor 1,5V
- Taster
Ich habe folgende Scripte kombiniert
**https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing**__
**http://starthardware.org/lektion-12-der-taster-und-if-abfrage/**__
Adafruit_Motor_Shield_V2_Library/DCMotorTest.ino at master · adafruit/Adafruit_Motor_Shield_V2_Library · GitHub
Mein Code
```
**/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control
For use with the Adafruit Motor Shield v2
----> Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit [v2.3] : ID 1438 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3);
Adafruit_DCMotor *myMotor4 = AFMS.getMotor(4);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
int tasterPin = 13;
int ledPin = 2;
// so now we have 3 motors called "motor", "motor2" and "motor3"
// Variables will change:
int motorState = 0;
int motorState2 = 0;
int motorState3 = 0;
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time motor1 was updated
long previousMillis2 = 0; // will store last time motor3 was updated
long previousMillis3 = 0; // will store last time motor4 was updated
long previousMillis4 = 0; // will store last time LED was updated
//Intervall für ersten Motor
unsigned long intervalon = 0; // interval at which to blink (milliseconds)
unsigned long intervaloff = 0;
//Intervall für zweiten Motor
unsigned long intervalon2 = 0; // interval at which to blink (milliseconds)
unsigned long intervaloff2 = 0;
//Intervall für zweiten Motor
unsigned long intervalon3 = 0; // interval at which to blink (milliseconds)
unsigned long intervaloff3 = 0;
//Intervall für Kamera
unsigned long intervalon4 = 0; // interval at which to blink (milliseconds)
unsigned long intervaloff4 = 0;
const int NUMBER_OF_FIELDS = 9; // Wie viele kommaseparierte Felder erwarten wir?
int fieldIndex = 0; // Das aktuell empfangene Feld
int values[NUMBER_OF_FIELDS]; // Array mit den Werte aller Felder
void readSerialString () {
if( Serial.available()) {
for(fieldIndex = 0; fieldIndex < NUMBER_OF_FIELDS; fieldIndex ++)
{
values[fieldIndex] = Serial.parseInt(); // Numerischen Wert einlesen
}
Serial.print( fieldIndex);
Serial.println(" Felder empfangen:");
for(int i=0; i < fieldIndex; i++)
{
Serial.println(values[i]);
}
//fieldIndex = 0; // und von vorn anfangen
}
}
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Adafruit Motorshield v2 - DC Motor test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Set the speed to start, from 0 (off) to 255 (max speed)
myMotor->setSpeed(150);
myMotor->run(FORWARD);
// turn on motor
myMotor->run(RELEASE);
myMotor3->setSpeed(150);
myMotor3->run(FORWARD);
// turn on motor
myMotor3->run(RELEASE);
myMotor4->setSpeed(150);
myMotor4->run(FORWARD);
// turn on motor
myMotor4->run(RELEASE);
pinMode(tasterPin,INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
readSerialString();
if (values[0] ==1){
//Intervall für ersten Motor
long intervalon = values[1] * 1000; // interval at which to blink (milliseconds)
long intervaloff = values[2] * 1000;
//Intervall für zweiten Motor
long intervalon2 = values[3] * 1000; // interval at which to blink (milliseconds)
long intervaloff2 = values[4] * 1000;
//Intervall für dritten Motor
long intervalon3 = values[5] * 1000; // interval at which to blink (milliseconds)
long intervaloff3 = values[6] * 1000;
//Intervall für Kamera
long intervalon4 = values[7] * 1000; // interval at which to blink (milliseconds)
long intervaloff4 = values[8] * 1000;
//Motor 1
unsigned long currentMillis = millis();
if((motorState == 1) && (currentMillis - previousMillis >= intervalon) && (digitalRead(tasterPin)==HIGH)) {
previousMillis = currentMillis;
motorState = 0;
myMotor->run(FORWARD);
//digitalWrite(ledPin, ledState);
Serial.println("Motor1 is on");
}
else if((motorState == 0) && (currentMillis - previousMillis >= intervaloff) || (digitalRead(tasterPin)==LOW)) {
previousMillis = currentMillis;
motorState = 1;
myMotor->run(RELEASE);
//digitalWrite(ledPin, ledState);
Serial.println("Motor1 is off");
}
//Motor 3
unsigned long currentMillis2 = millis();
if((motorState2 == 1) && ( currentMillis2 - previousMillis2 >= intervalon2) && (digitalRead(tasterPin)==HIGH)) {
previousMillis2 = currentMillis2;
motorState2 = 0;
myMotor3->run(FORWARD);
//digitalWrite(ledPin, ledState);
Serial.println("Motor3 is on");
}
else if((motorState2 == 0) && (currentMillis2 - previousMillis2 >= intervaloff2) || (digitalRead(tasterPin)==LOW)){
previousMillis2 = currentMillis2;
motorState2 = 1;
myMotor3->run(RELEASE);
//digitalWrite(ledPin, ledState);
Serial.println("Motor3 is off");
}
//Motor 4
unsigned long currentMillis3 = millis();
if((motorState3 == 1) && (currentMillis3 - previousMillis3 >= intervalon3) && (digitalRead(tasterPin)==HIGH)) {
previousMillis3 = currentMillis3;
motorState3 = 0;
myMotor4->run(FORWARD);
//digitalWrite(ledPin, ledState);
Serial.println("Motor4 is on");
}
else if((motorState3 == 0) && ( currentMillis3 - previousMillis3 >= intervaloff3) || (digitalRead(tasterPin)==LOW)){
previousMillis3 = currentMillis3;
motorState3 = 1;
myMotor4->run(RELEASE);
//digitalWrite(ledPin, ledState);
Serial.println("Motor4 is off");
}
//LED
unsigned long currentMillis4 = millis();
if((ledState == HIGH) && ( currentMillis4 - previousMillis4 >= intervalon4) && (digitalRead(tasterPin)==HIGH)) {
previousMillis4 = currentMillis4;
ledState = LOW;
digitalWrite(ledPin, HIGH);
Serial.println("Camera is on");
}
else if((ledState == LOW) && (currentMillis4 - previousMillis4 >= intervaloff4) || (digitalRead(tasterPin)==LOW)){
previousMillis4 = currentMillis4;
ledState = HIGH;
digitalWrite(ledPin, LOW);
Serial.println("Camera is off");
}
}
}
__```__
Per Bluetooth sende ich folgende Werte
__> 1,5,5,5,5,5,5,5,5**__
Per Serial Print erhalte ich immer
> Motor1 is off
> Motor2 is off
> Motor3 is off
> Motor4 is off
> Camera is off
Was kann den falsch an der If-Abfrage sein?