简体   繁体   中英

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.

The problem is that all the Java program is reading is the first message sent in the setup() over and over.

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. The Arduino code is below.

  #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. I can include it if necessary though.

The Java console output appears as:

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?

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 is now reset at the end of the loop() ;
  • String object and manipulations were replaced by heap-friendlier code.
  • Added 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; they return short int s, I think.

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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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