User input stuck in loop - input

I have made this code on Arduino where the objective is to have the user type in a delay time into the serial monitor, and then the LED should be blink with that delay time. For example if I type in 1000 the LED should be turned on for 1 second then off for 1 second, then repeat.
My problem is that when the code has finished running once, it waits for a new user input, instead of continuing to blink. I think i have to take the Serial.parseInt out of the loop but i'm not sure how as every time I have tried to put it somewhere else the LED just lights up constantly.
Here is the code:
int ledPin = 13;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.print(" Enter delay time: ");
while (!Serial.available());
}
void loop() {
// put your main code here, to run repeatedly
int delayTime = Serial.parseInt();
digitalWrite(ledPin, HIGH);
delay(delayTime);
digitalWrite(ledPin, LOW);
delay(delayTime);
}

Serial.parseInt is a blocking function. That means it waits for valid serial input until it times out. Because of this, any other action in loop has to wait too. Reading user input in setup works only once though, so it never asks the user for input again.
To avoid this, you'll have to check the serial buffer, and then read each byte individually, while also doing the LED blinking in the main loop.
Another thing to avoid now, is the use of the delay function, because it also hangs the entire main loop (including the serial readings) for the given parameter time. You can still blink the LED by using timestamp intervals.
For a nice example of a non-blocking serial read, we can use this sample from the Arduino docs. Additionally, for another nice example of an LED-blinking sketch without using delay, we can use the BlinkWithoutDelay sample from the Arduino docs too.
String inString = "";
unsigned long previousMillis = 0;
int delayTime = 0;
int ledState = LOW;
int ledPin = 13;
void nonBlockingSerialReadDelayTime() {
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string
inString += (char)inChar;
}
// if you get a newline (user pressed ENTER on the serial console)
if (inChar == '\n') {
// set our new delay time
delayTime = inString.toInt();
// clear the string for new input
inString = "";
// ask user again
Serial.print(" Enter delay time: ");
}
}
}
void blinkLED() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= delayTime) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable
digitalWrite(ledPin, ledState);
}
}
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
while (!Serial.available());
Serial.print(" Enter delay time: ");
}
void loop() {
nonBlockingSerialReadDelayTime();
blinkLED();
}

Simply read the delay time in your setup befor you enter loop
int ledPin = 13;
int delayTime = 0;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.print(" Enter delay time: ");
while (!Serial.available());
delayTime = Serial.parseInt();
}
void loop() {
// put your main code here, to run repeatedly
digitalWrite(ledPin, HIGH);
delay(delayTime);
digitalWrite(ledPin, LOW);
delay(delayTime);
}

Sure Serial.parseInt() is blocking, but you can combine it with Serial.available()
const int ledPin = 13;
int delayTime = 1000;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.print(" Enter delay time: ");
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(delayTime);
digitalWrite(ledPin, LOW);
delay(delayTime);
if (Serial.available()) {
int temp = Serial.parseInt();
if (temp > 0) delayTime = temp;
Serial.print(" Enter delay time: ");
}
}
Of course this approach does not allow to break into a very slow blink cycle immediately, but that's a different issue.

Related

How do you make a button only send one input to a source while being pushed down and not send another one until the button is let go and repressed?

In a basic variable and Button setup on an Arduino software, the idea is that if you press a button, you gain one on a variable. Theoretically, holding the button shouldn't cause the variable to increase more than one until the release and repressing of the button. However, I could not make this happen. How is this supposed to be done?
I tried using an else around a delay (because the adding one to a variable code was inside an "if" statement) so it would not delay unless the button and released and would not count multiple button inputs, but this resulted in a larger increase when a button was clicked rather than just a one increase.
const int buttonPin = 8;
int number = 0;
int numbertwo = 0;
int buttonState = 0;
void setup()
{
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW){
numbertwo = ++number;
delay(100);
}
(Can use any method to determine variable, like on an LCD)
As mentioned above, I expected the variable to only increase by one when a button is pressed and not to continue increasing until the button is released and re-pressed, but what actually happened was the variable kept increasing in value as the button was held.
I'm assuming you want the variable "number" increase by 1 per press. I think you are looking for something like this:
const int buttonPin = 8;
int number = 0;
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW){
++number;
// This loop is to make sure not to count up until button is released
do {
delay(5);
buttonState = digitalRead(buttonPin);
} while (buttonState == LOW);
}
}
If you don't like the loop based implementation, here's a state based implementation for you:
const int buttonPin = 8;
int number = 0;
int buttonState = 0;
int prevState = -1;
void setup() {
pinMode(buttonPin, INPUT);
prevState = -1;
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && prevState != buttonState){
++number;
prevState = buttonState;
}
delay(5);
}

Mpu6050 and Adafruit Ultimate Gps not working together on Arduino Due

I have the codes for mpu6050 and adafruit ultimate gps breakout v3 and they are working fine seperately on arduino due but when i try to combine both the codes the gps does not get a fix. Can anybody help me out?
The code for mpu6050 is given below
// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
#include<Wire.h>
extern TwoWire Wire1;
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
double pitch,roll,delta_X,delta_Y,delta_Z;
double old_AcX=0;
double old_AcY=0;
double old_AcZ=0;
int led = 13;
void setup(){
Wire1.begin();
Wire1.beginTransmission(MPU_addr);
Wire1.write(0x6B); // PWR_MGMT_1 register
Wire1.write(0); // set to zero (wakes up the MPU-6050)
Wire1.endTransmission(true);
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop(){
Wire1.beginTransmission(MPU_addr);
Wire1.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire1.endTransmission(false);
Wire1.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=Wire1.read()<<8|Wire1.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire1.read()<<8|Wire1.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire1.read()<<8|Wire1.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire1.read()<<8|Wire1.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire1.read()<<8|Wire1.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire1.read()<<8|Wire1.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire1.read()<<8|Wire1.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print("AcX = "); Serial.print(AcX);
Serial.print(" | AcY = "); Serial.print(AcY);
Serial.print(" | AcZ = "); Serial.print(AcZ);
Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet
Serial.print(" | GyX = "); Serial.print(GyX);
Serial.print(" | GyY = "); Serial.print(GyY);
Serial.print(" | GyZ = "); Serial.println(GyZ);
delay(1000);
}
And the code for the Adafruit ultimate Gps breakout is given below
#include <Adafruit_GPS.h>
#define mySerial Serial1
Adafruit_GPS GPS(&mySerial);
#define GPSECHO true
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
void setup()
{
Serial.begin(9600);
GPS.begin(9600);
mySerial.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
GPS.sendCommand(PGCMD_ANTENNA);
#ifdef __arm__
usingInterrupt = false;
#else
useInterrupt(true);
#endif
delay(1000);
}
#ifdef __AVR__
SIGNAL(TIMER0_COMPA_vect) {
char c = GPS.read();
#ifdef UDR0
if (GPSECHO)
if (c) UDR0 = c;
// writing direct to UDR0 is much much faster than Serial.print
// but only one character can be written at a time.
#endif
}
void useInterrupt(boolean v) {
if (v) {
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
usingInterrupt = true;
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
usingInterrupt = false;
}
}
#endif //#ifdef__AVR__
uint32_t timer = millis();
void loop()
{
if (! usingInterrupt) {
char c = GPS.read();
}
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trytng to print out data
//Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// if millis() or timer wraps around, we'll just reset it
if (timer > millis()) timer = millis();
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
Serial.print("\nTime: ");
Serial.print(GPS.hour, DEC); Serial.print(':');
Serial.print(GPS.minute, DEC); Serial.print(':');
Serial.print(GPS.seconds, DEC); Serial.print('.');
Serial.println(GPS.milliseconds);
Serial.print("Date: ");
Serial.print(GPS.day, DEC); Serial.print('/');
Serial.print(GPS.month, DEC); Serial.print("/20");
Serial.println(GPS.year, DEC);
Serial.print("Fix: "); Serial.print((int)GPS.fix);
Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
if (GPS.fix) {
//Serial.print("Location: ");
Serial.print(convertDegMinToDecDeg(GPS.latitude));
Serial.print(", ");
Serial.println(convertDegMinToDecDeg(GPS.longitude));
//Serial.print("Speed (knots): "); Serial.println(GPS.speed);
//Serial.print("Angle: "); Serial.println(GPS.angle);
//Serial.print("Altitude: "); Serial.println(GPS.altitude);
//Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
}
}
}
Both the codes are working fine separetely but i am unable to combine them and run in a single code.I tried to combine them and tha adafruit Ultimate gps breakout isn't working and it gives nothing. I want to know how i can combine them to work in a single code.Thanks in advance.
Use NeoGPS instead -- just add it to your IMU sketch:
#include <NMEAGPS.h>
NMEAGPS gps;
#define gpsPort Serial1
...
void setup(){
Wire1.begin();
Wire1.beginTransmission(MPU_addr);
Wire1.write(0x6B); // PWR_MGMT_1 register
Wire1.write(0); // set to zero (wakes up the MPU-6050)
Wire1.endTransmission(true);
Serial.begin(9600);
pinMode(led, OUTPUT);
gpsPort.begin( 9600 );
}
void loop(){
if (gps.available( gpsPort )) {
gps_fix fix = gps.read(); // A new GPS update is ready, get all the pieces
// Print some of the pieces?
Serial.print( F("Location: ") );
if (fix.valid.location) {
Serial.print( fix.latitude(), 6 );
Serial.print( ',' );
Serial.print( fix.longitude(), 6 );
}
Serial.print( F(", Altitude: ") );
if (fix.valid.altitude)
Serial.print( fix.altitude() );
Serial.println();
// Take an IMU sample too.
Wire1.beginTransmission(MPU_addr);
...
Serial.print(" | GyZ = "); Serial.println(GyZ);
}
}
This will display one GPS update and one IMU sample per second.
Also, you cannot use delay. The Arduino will not do anything else during the delay, and it will lose GPS characters. Notice that the above loop structure is always running, checking for GPS data. When a GPS update is finally ready, it takes the IMU sample and prints all the results.
You also have to be careful about printing too much information. Eventually, the Arduino will spend all its time waiting to print characters.
NeoGPS is available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries. NeoGPS is faster, smaller, more reliable and more accurate than all other GPS libraries, and the examples are properly structured. It is very common for the other libraries' examples to break when they are modified. Even if you don't use it, there is lots of information on the NeoGPS Installation and Troubleshooting pages.

P5.js and Tone.js Can't Connect to Arduino

I'm having trouble displaying an image and playing audio using p5 when my arduino red led goes off. My arduino works, I just can't figure out how to get an image to pop up and audio to play when the red led goes off. My project is basically a motion sensor using an ultrasonic sensor, if that helps.
Thank you for your time.
Here's my sketch.js code:
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/cu.usbmodem1421'; // fill in your serial port name here
var synth;
function preload() {
alert = loadImage('alert.jpeg');
}
function setup() {
createCanvas(1920,1080);
serial = new p5.SerialPort(); // make a new instance of the serialport
library
serial.on('list', printList); // set a callback function for the serialport
list event
serial.on('connected', serverConnected); // callback for connecting to the
server
serial.on('open', portOpen); // callback for the port opening
serial.on('data', serialEvent); // callback for when new data arrives
serial.on('error', serialError); // callback for errors
serial.on('close', portClose); // callback for the port closing
serial.list(); // list the serial ports
serial.open(portName); // open a serial port
var synth = new Tone.Synth().toMaster();
synth.triggerAttackRelease(440, 2);
var player = new Tone.Player("IntruderAlert.mp4").toMaster();
//play as soon as the buffer is loaded
player.autostart = true;
player.playbackRate = 0.8;
}
function serverConnected() {
println('connected to server.');
}
function portOpen() {
println('the serial port opened.')
}
function serialEvent() {
}
function serialError(err) {
println('Something went wrong with the serial port. ' + err);
}
function portClose() {
println('The serial port closed.');
}
Here's my .ino code:
#define trigPin 6<br>#define echoPin 7
#define GreenLED 11
#define YellowLED 10
#define RedLED 9
#define buzzer 3
int sound = 500;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(GreenLED, OUTPUT);
pinMode(YellowLED, OUTPUT);
pinMode(RedLED, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/5) / 29.1;
if (distance < 50) {
digitalWrite(GreenLED, HIGH);
}
else {
digitalWrite(GreenLED, LOW);
}
if (distance < 20) {
digitalWrite(YellowLED, HIGH);
}
else {
digitalWrite(YellowLED,LOW);
}
if (distance < 5) {
digitalWrite(RedLED, HIGH);
sound = 1000;
}
else {
digitalWrite(RedLED,LOW);
}
if (distance > 5 || distance <= 0){
Serial.println("Out of range");
noTone(buzzer);
}
else {
Serial.print(distance);
Serial.println(" cm");
tone(buzzer, sound);
}
delay(300);
}
You're going to have to break your problem down into smaller steps and then approach those steps one at a time.
Can you create a sketch that just shows an image? Forget about the Arduino for a second, and just display an image. Now can you display an image when the user clicks on the screen?
Separately from that, can you write Arduino code that simply sends a message to a sketch when the red light goes off?
When you get those working independently, then it'll be much easier to think about combining them into a single application that combines both ideas.
If you get stuck, please post a MCVE of the specific step that you're stuck on, and we'll go from there. Good luck.

Arduino Protothreads seem to be shared by two buttons

I ran into a problem regarding the Protothreading library in Arduino. I have created a Button class, which represents a hardware button. Now the idea is that you can attach a ButtonListener to it, which listens to the button. If a button is pressed, then the clicked() function is called.
#include <Arduino.h>
#include <pt.h>
class ButtonListener {
public:
virtual void clicked() = 0;
virtual void longClicked() = 0;
virtual void tapped(int) = 0;
};
class Button {
static const int RECOIL_TIME = 200;
static const int LONG_CLICK_LENGTH = 1000;
private:
int _pin;
ButtonListener *_listener;
struct pt _thread;
unsigned long _timestamp = 0;
int listenerHook(struct pt *pt) {
PT_BEGIN(pt);
this->_timestamp = 0;
while (true) {
PT_WAIT_UNTIL(pt, millis() - _timestamp > 1);
_timestamp = millis();
if (&this->_listener != NULL) {
this->listenForClick();
}
}
PT_END(pt);
}
void listenForClick() {
boolean longClicked = true;
int state = digitalRead(this->_pin);
if (state == HIGH) {
unsigned long timestamp = millis();
while (true) {
longClicked = millis() - timestamp > LONG_CLICK_LENGTH;
state = digitalRead(this->_pin);
if (state == LOW) {
break;
}
}
if (&this->_listener != NULL) {
if (longClicked) {
(*this->_listener).longClicked();
}
else {
(*this->_listener).clicked();
}
}
}
}
public:
Button(int pin) {
this->_pin = pin;
}
void init() {
pinMode(this->_pin, OUTPUT);
PT_INIT(&this->_thread);
}
void setListener(ButtonListener *listener) {
this->_listener = listener;
}
void listen() {
this->listenerHook(&this->_thread);
}
};
Now I've created two implementations of ButtonListener:
class Button12Listener : public ButtonListener {
public:
void clicked() {
Serial.println("Button 12 clicked!");
}
}
The other implementation is a Button13Listener and prints "Button 13 clicked!"
Then let's run the code:
// Instantiate the buttons
Button button12(12);
Button button13(13);
void setup() {
Serial.begin(9600);
button12.init();
button13.init();
// Add listeners to the buttons
button12.setListener(new Button12Listener());
button13.setListener(new Button13Listener());
}
void loop() {
while (true) {
// Listen for button clicks
button12.listen();
button13.listen();
}
Serial.println("Loop ended.");
delay(60000);
}
I expect "Button 12 clicked!" when I click the button on pin 12, and "Button 13 clicked!" when I click the button on pin 13.
But when I try to click on any of the buttons, it is randomly printing "Button 12 clicked!" or "Button 13 clicked!" no matter what button I press.
It look like the protothreads are shared among the buttons or something.
If I check in which order the buttons are called, like this:
button12.listen();
Serial.println("listen12");
button13.listen();
Serial.println("listen13");
then the following outputs:
12
13
12
13
12
12
Thát seems okay.
So what's the problem? What have I missed?
You are completely eliminating the whole point of protothreads by having that while(true) loop in listenForClick. I would do it like this:
PT_BEGIN(thr);
while(1){
// ensure that the pin is low when you start
PT_WAIT_UNTIL(thr, digitalRead(pin) == LOW);
// wait until pin goes high
PT_WAIT_UNTIL(thr, digitalRead(pin) == HIGH);
// insert delay here for minimum time the pin must be high
this->timeout = millis() + 20; // 20 ms
// wait until the delay has expired
PT_WAIT_UNTIL(thr, this->timeout - millis() > 0);
// wait until the pin goes low again
PT_WAIT_UNTIL(thr, digitalRead(pin) == LOW);
// call the click callback
this->clicked();
}
PT_END(thr);
Then just call this thread repeatedly.
NOTE: when you have buttons connected, you would usually have pullup on the pin and have button connected between the pin and ground - so the pin is LOW when the button is down and high when it is not being pressed. This would certainly be the case on an arduino. So you would have to change the code above to wait for a negative pulse instead of a positive one. :)

SD card error for Arduino Mega

I am trying to run a simple program using a SD card module from spark fun with a Arduino Mega2560. This is the program i am current using:
#include <SD.h>
const int chipSelect = 10;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(53, OUTPUT);
digitalWrite(10, HIGH);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
String dataString = "";
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error opening datalog.txt");
}
}
The error i am receiving says :
Initializing SD card...Card failed, or not present
error opening datalog.txt
Change
pinMode(53, OUTPUT);
digitalWrite(10, HIGH);
To
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);
And have a try.
Sometimes it may happen for not writing this
File dataFile = SD.open("datalog.txt", FILE_WRITE);
immediately after this [or long before writing to that file]
Serial.println("card initialized.");
Believe me, this happened to me.