Stránka 1 z 1

Krokový motor přes bluetooth

Napsal: 14 srp 2022, 13:17
od sleepblue
Ahoj, po zaslání "1" přes bt modul by měl krokový motor udělat otáčku tam počkat a vrátit se zpět.
Samotné otáčení je ok, nebo například ovládání led diody, ale ovládání nevím jak propojit. Pomůže někdo prosím?

Kód: Vybrat vše

#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200
int state = 0;

void setup() {
  // Declare pins as output:
  
  Serial.begin(9600); // Default communication rate of the Bluetooth module
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(dirPin, LOW);
  
  for (int i = 0; i < 1 * stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
  delay(3000);
  // Set the spinning direction counterclockwise:
  digitalWrite(dirPin, HIGH);
  
  for (int i = 0; i < 1 * stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
  delay(500);
}

void loop() {
  if(Serial.available() > 0){ // Checks whether data is comming from the serial port
    state = Serial.read(); // Reads the data from the serial port
 }
  if (state == '1') {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  state = 0;
 } 
}
PS: Pro kód používej značku code (v hranatých závorkách), nebo tlačítko </>
Gilhad

Re: Krokový motor přes bluetooth

Napsal: 14 srp 2022, 19:53
od jankop
Trochu ti nerozumím, ale pokusil jsem se na tvůj dotaz reagovat. Když tak upřesni, o co ti přesně jde.

Kód: Vybrat vše

#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200
int state = 0;

void setup() {
// Declare pins as output:

Serial.begin(9600); // Default communication rate of the Bluetooth module
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(dirPin, LOW);
}
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
}
if (state == '1') {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(dirPin, LOW);
for (int i = 0; i < 1 * stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
delay(3000);
// Set the spinning direction counterclockwise:
digitalWrite(dirPin, HIGH);

for (int i = 0; i < 1 * stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
state = 0;
digitalWrite(LED_BUILTIN, LOW);
}
}