简体   繁体   English

Arduino for 循环完全不起作用

[英]Arduino for loop completely not functional

I am trying to send data from an accelerometer to Java from an Arduino. I am using delta time to limit it to sending only every 250 ms.我正在尝试从 Arduino 将数据从加速度计发送到 Java。我正在使用增量时间将其限制为每 250 毫秒发送一次。

The problem is that all the Java program is reading is the first message sent in the setup() over and over.问题是所有 Java 程序正在读取的是setup()中发送的第一条消息一遍又一遍。

I added a test Serial.write to check if the program is ever entering the delta time block, and it seems to be sending (or at least, reading) the first 2 characters of that message.我添加了一个测试Serial.write来检查程序是否曾经进入增量时间块,它似乎正在发送(或至少读取)该消息的前 2 个字符。 The Arduino code is below. Arduino 代码如下。

  #include <SparkFun_MMA8452Q.h>

 int sleepPin = 7;
 int stepPin = 6;
 int buttonPin = 8;
 int stepCount = 0;
 boolean stepMode = true;
 int delTime = 5000;
 MMA8452Q accel; //accelerometer

 void setup() {
   Serial.begin(9600);
   while (millis() < 4000); //wait so I can start java program
   Serial.write("Connected");

   //set pins
   pinMode(sleepPin, OUTPUT);
   pinMode(stepPin, OUTPUT);
   pinMode(buttonPin, INPUT);

   digitalWrite(stepPin, HIGH); //starts in step mode

   delTime = millis() + 250;
 }

 void loop() {

   if (digitalRead(buttonPin) == HIGH) stepMode = !stepMode;

   if (millis() > delTime) {
     Serial.write("delTime"); //test case

     //set led's according to mode
     if (stepMode) {
       digitalWrite(stepPin, HIGH);
       digitalWrite(sleepPin, LOW);
     } else {
       digitalWrite(stepPin, HIGH);
       digitalWrite(sleepPin, LOW);
     }
    
     //create string to store data
     String data = "";
     if (stepMode) data += "s"; //s is step mode key
     else data += "z"; //z is sleep mode key

     //add actual reading stuff
     data += String(accel.getX()) + "," + String(accel.getY());
     Serial.write(data.c_str()); //send the lad over
   }
 }

The Java side is nearly identical (sans the conditions of an if statement, but it doesn't matter because if it doesn't meet the statement it just prints what it sees) to a functional program for serial communication that I've used before. Java 端与我之前使用过的用于串行通信的功能程序几乎相同(没有 if 语句的条件,但这并不重要,因为如果它不满足语句,它只会打印它看到的内容) . I can include it if necessary though.如有必要,我可以包括它。

The Java console output appears as: Java 控制台 output 显示为:

Connected
de
Connected
de
Connected
de

where a new iteration appears about once a second.大约每秒出现一次新的迭代。 What am I doing that prevents the Arduino from sending the data?我在做什么阻止 Arduino 发送数据?

Not a proper answer yet, more of a test, but I couldn't fit it in a comment.还不是一个正确的答案,更多的是测试,但我无法将其放入评论中。

Changes made:所做的更改:

  • delTime is now an unsigned long int ; delTime现在是一个unsigned long int
  • delTime is now reset at the end of the loop() ; delTime现在在loop()结束时重置;
  • String object and manipulations were replaced by heap-friendlier code. String object 和操作被堆友好代码取代。
  • Added accel.begin();添加accel.begin();

Let me know if this works for you, and if not, where it complains.让我知道这是否适合您,如果不适合,请告诉我它在哪里抱怨。 Haven't fully tested the code.还没有完全测试代码。 You could also try replacing accel.getX() and accel.getY() with numbers;您也可以尝试用数字替换accel.getX()accel.getY() they return short int s, I think.他们返回short int s,我想。

BTW the button needs debouncing.顺便说一句,按钮需要去抖动。

#include <SparkFun_MMA8452Q.h>

int sleepPin = 7;
int stepPin = 6;
int buttonPin = 8;
boolean stepMode = true;
unsigned long int delTime = 0;
MMA8452Q accel; //accelerometer

void setup(){
  Serial.begin(9600);
  while(millis() < 4000); //wait so I can start java program
  Serial.write("Connected");

  //set pins
  pinMode(sleepPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  accel.begin();

  digitalWrite(stepPin, HIGH); //starts in step mode

  delTime = millis() + 250;
}

void loop() {
  char str[15];
  if (digitalRead(buttonPin) == HIGH)
    stepMode = !stepMode;
  if (millis() > delTime) {
    //set led's according to mode
    if (stepMode) {
      digitalWrite(stepPin, HIGH);
      digitalWrite(sleepPin, LOW);
      Serial.write('s');
    } else {
      digitalWrite(stepPin, HIGH);
      digitalWrite(sleepPin, LOW);
      Serial.write('z');
    }
    
    sprintf(str, "%d", accel.getX());
    Serial.write(str);
    Serial.write(',');
    sprintf(str, "%d", accel.getY());
    Serial.write(str);
    Serial.write('\n');

    delTime = millis() + 250;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM