Arduino serial input prompt not repeat - input

Following Arduino code purpose is to get a char from user.
Currently problem of continuously prints the prompt.
I want it to print the prompt only once, then wait for the input.
Thanks.
char kbChar;
void setup() {Serial.begin(9600);}
void loop() {inChar();}
void inChar(){
Serial.println("Type something!");
if(Serial.available()){
kbChar = Serial.read();
Serial.println(kbChar);
}
}

Note the following attributes about the setup() and loop() functions in an Arduino sketch:
setup() is called once, at the very beginning
loop() is called repeatedly, forever, after setup() finishes.
Therefore, you are continuing to print the prompt, because your prompt call Serial.println("Type something!"); is inside of inChar(), which is called in the loop() function body. To make the prompt output only once, put it in the setup() function body.
char kbChar;
void setup() {
Serial.begin(9600);
while (!Serial) ; // Wait for Serial port to open.
Serial.println("Type something!");
}
void loop() {
inChar();
}
void inChar(){
if(Serial.available()){
kbChar = Serial.read();
Serial.println(kbChar);
}
}

Related

Scanner not waiting for user input - IntelliJ IDEA

Super newbie question here.
I'm taking a course on Udemy and trying to run some of the scripts from the lessons I've taken. As far as I can see, I've entered the script exactly as it's in the lesson video.
Regardless, for some reason, the script just barrels through all of the lines containing Scanner, takes no user input, and somehow runs to the script's end.
I'm using IntelliJ IDEA for all of this. When I run the script, a console window does not even appear for user input.
Below is an example script:
import java.util.Scanner;
public class J4AB_S6_L4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("The VIP Lounge");
System.out.print("Age:");
int age = scanner.nextInt();
if (age >= 18) {
System.out.println("Do you have a VIP pass? yes/no:");
String vipPassReply = scanner.next();
if (vipPassReply.equals("yes")) {
System.out.println("Thanks, go in.");
}
else {
System.out.println("Sorry you must have a VIP Pass.");
}
}
}
And another script, using Switch.
import java.util.Scanner;
public class J4AB_S6_L7 {
// This is an intro to switch statement syntax.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
switch (num) {
case 1:
System.out.println("You entered one.");
break;
case 2:
System.out.println("You entered two.");
break;
default:
System.out.println("Dude, why'd you enter that?");
break;
}
}
Why is the script just running through without any user input? Is this a matter of the script itself or IntelliJ IDEA?

Aborting worker object in QThread

I know that there are many posts on this topic and i've read them and thought i also understood them. But I am still having a problem with aborting a QThread or rather a worker object in a QThread.
I have an GUI application and a library. The GUI can request the library to exec and abort worker objects (is connected to WorkerHandler slots). The WorkerHandler can create several WorkerObjects that all inherit from a base class. I tried to reduce the code for this example, but it's still some kind of verbose.
Gui.h
class Gui : public QMainWindow
{
Q_OBJECT
public:
Gui(QWidget *parent = 0);
~Gui();
private:
Ui::GuiClass ui;
QThread *workerHandlerThread;
WorkerHandler *workerHandler;
void connectActions();
signals:
void execWorker(WorkerParams _params);
void abortWorker(WorkerType type);
slots:
void buttonExecPressed();
void buttonAbortPressed();
}
Gui.cpp
void Gui::Gui()
{
ui.btnExecA->setProperty("type", QVariant::fromValue(WorkerType::A)); //WorkerType is just a enum, bin type to button
ui.btnExecB->setProperty("type", QVariant::fromValue(WorkerType::B));
ui.btnAbortA->setProperty("type", QVariant::fromValue(WorkerType::A));
ui.btnAbortB->setProperty("type", QVariant::fromValue(WorkerType::B));
connectActions();
workerHandlerThread = new QThread();
workerHandler = new WorkerHandler();
workerHandler->moveToThread(workerHandlerThread); // move worker execution to another thread
workerHandlerThread->start(); //start will call run and run will run the QEventLoop of QThread by calling exec
}
void Gui::~Gui()
{
workerHandlerThread->quit();
workerHandlerThread->wait();
delete workerHandlerThread;
delete workerHandler;
}
void Gui::connectActions()
{
connect(ui.btnExecA, &QPushButton::clicked, this, &Gui::buttonExecPressed);
connect(ui.btnExecB, &QPushButton::clicked, this, &Gui::buttonExecPressed);
connect(ui.btnAbortA, &QPushButton::clicked, this, &Gui::buttonAbortPressed);
connect(ui.btnAbortB, &QPushButton::clicked, this, &Gui::buttonAbortPressed);
connect(this, &Gui::execWorker, workerHandler, &WorkerHandler::execWorker);
connect(this, &Gui::abortWorker, workerHandler, &WorkerHandler::abortWorker);
}
void Gui::buttonExecPressed()
{
QPushButton* button = qobject_cast<QPushButton*>(sender());
if (button)
{
WorkerType type = button->property("type").value<WorkerType>(); //get worker type
WorkerParams params = WorkerParamsFactory::Get()->CreateParams(type); //WorkerParamsFactory cretes default parameters based on type
emit execWorker(params); //tell WorkerHandler to create a workerObject based on these parameters
}
}
void Gui::buttonAbortPressed()
{
QPushButton* button = qobject_cast<QPushButton*>(sender());
if (button)
{
WorkerType type = button->property("type").value<WorkerType>();
emit abortWorker(type); //tell WorkerHandler to abort a specific workerObject
}
}
WorkerHandler.h
class WorkerHandler : public QObject {
Q_OBJECT
public:
WorkerHandler(QObject * parent = Q_NULLPTR);
~WorkerHandler();
public slots:
void execWorker(WorkerParams _params);
void abortWorker(WorkerType type);
private:
QMap<WorkerType, WorkerObjectBase*> workerPool; //contains the workerobjects
};
WorkerHandler.cpp
void WorkerHandler::execWorker(WorkerParams _params)
{
QThread *thread = new QThread();
WorkerObjectBase *worker = WorkerObjectFactory::Get()->CreateWorker(_params); //Factory to create specific Worker Object based on given params
worker->moveToThread(thread);
connect(thread, &QThread::started, workerThread, &WorkerObjectBase::process);
connect(workerThread, &WorkerObjectBase::workerFinished, thread, &QThread::quit); //quit the QThread when worker is finished
connect(thread, &QThread::finished, thread, &QThread::deleteLater); //free resources when thread is finished
connect(thread, &QThread::finished, workerThread, &WorkerObjectBase::deleteLater); //free resources when thread is finished
workerPool.insert(_params.type, worker); //_params.type contains WorkerType
thread->start(); //will call run of qthread which will call exec
}
void WorkerHandler::abortWorker(WorkerType type)
{
WorkerObjectBase *worker = workerPool.value(type);
worker->requestAbort();
QThread *workerThread = worker->thread();
if (workerThread)
{
if (!workerThread->wait(10000)) //will always block the 10 seconds and terminate the thread. using just wait() will block forever
{
workerThread->terminate();
}
}
}
WorkerHandlerBase.h
class WorkerObjectBase : public QObject {
Q_OBJECT
public:
WorkerObjectBase(QObject * parent = Q_NULLPTR);
~WorkerObjectBase();
void requestAbort();
protected:
//some WorkerObject basic parameters
bool abortRequested();
public slots:
virtual void process();
signals:
void workerFinished();
private:
QMutex abortMutex;
bool abort = false;
};
WorkerHandlerBase.cpp
void WorkerObjectBase::requestAbort()
{
abortMutex.lock();
abort = true;
abortMutex.unlock();
}
bool WorkerObjectBase::abortRequested()
{
bool abortRequested;
abortMutex.lock();
abortRequested = abort;
abortMutex.unlock();
return abortRequested;
}
WorkerObjectA.h
class WorkerObjectA : public WorkerObjectBase {
Q_OBJECT
public:
WorkerObjectA(QObject * parent = Q_NULLPTR);
~WorkerObjectA();
protected:
//some WorkerObjectA parameters
public slots:
void process();
};
WorkerObjectA.cpp
void WorkerObjectA::process()
{
while(!abortRequested())
{
//do some stuff
}
emit workerFinished();
}
The problem is, when i use wait, it blocks the signal processing. workerFinished is not handled and QThread does not quit. But I still don't get why. When i create a new worker object, i move it to a different thread. When this thread is started, it runs its own QEventLoop as stated in QThread
5.5 Documentation:
void QThread::run()
The starting point for the thread. After calling start(), the newly
created thread calls this function. The default implementation simply
calls exec().
So even if my WorkerHandler thread is blocking because of calling wait, the QThread of the specific workerObject should still manage to get the workerFinished signal and call the quit slot. If i don't use wait at all, everything is fine. But when something unexpected happens in the worker object process method that keeps it from emitting workerFinished, i want to be able to kill the thread the hard way.
So, what am i doing wrong?

How do I implement a mouse event outside the Java frame?

I am trying to make a return program which will press the enter key when the left mouse key is clicked...
with courtesy of
http://www.java-tips.org/java-se-tips/java.awt/how-to-use-robot-class-in-java.html (for void typing method) and "thenewboston" I have gotten so far...
I am trying to make it so that it will function in other platforms, for example: Word, Note Pad and not just on a JFrame
This is what I have up till now...
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class MW3Tool
{
public static void main (String[] args)
{
Robot enter = new Robot();
int num;
return count = 0;
num = count * 3;
Control c = new Control();
for (int k = 1; k <= num; k++)
{System.out.println("H");}
/* try {
Robot robot = new Robot(); // Going to be used to electronically hit the enter key later.
robot.delay(5000);
robot.setSpeed(10);
for (int k = 1; k<= num; k ++)
robot.keyPress(KeyEvent.VK_ENTER);
}
catch (AWTException e) {
e.printStackTrace();
} } */
}
private class Control implements MouseListener
{
int count;
int useless;
int useless2;
public void mouseClicked(MouseEvent event)
{
count++;
}
public void mousePressed(MouseEvent event)
{
useless++;
}
public void mouseExited(MouseEvent event)
{
useless2++;
}
}
}
My errors:
----jGRASP exec: javac -g MW3Tool.java
MW3Tool.java:20: cannot return a value from method whose result type is void
return count = 0;
^
MW3Tool.java:22: cannot find symbol
symbol : variable count
location: class MW3Tool
num = count * 3;
^
MW3Tool.java:35: non-static variable this cannot be referenced from a static context
Control c = new Control();
^
MW3Tool.java:60: MW3Tool.Control is not abstract and does not override abstract method mouseEntered(java.awt.event.MouseEvent) in java.awt.event.MouseListener
private class Control implements MouseListener
^
4 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Sorry for my inefficient methods (newly affiliated with Java)
Any help will be appreciated... thank you...
First error: Are you trying to initialize an Integer? Wrong syntax. Use
int count = 0;
Second error: Solving the first error will solve this error.
Third error: Instead of saying
private class Control implements MouseListener { ... }
Say
private static class Control implements MouseListener { ... }
Last error:
See the MouseListener Javadocs:
Method Summary
void mouseClicked(MouseEvent e)
Invoked when the mouse button has been clicked (pressed and released) on a component.
void mouseEntered(MouseEvent e)
Invoked when the mouse enters a component.
void mouseExited(MouseEvent e)
Invoked when the mouse exits a component.
void mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component.
void mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a component.
You MUST override all of these methods in the Control class for your program to work.
Hope this helps!

Java - pressing a direction key and having it move smoothly

When I press a direction key to move the object in that direction, it moves once, pauses momentarily, then moves again. Kind of like how if I want to type "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", I would hold "a" key down, but after the first "a" there is a pause then the rest of the "a"'s are typed. How do I remove that pause in KeyListener? Thank you.
This is the key repetition feature that the OS provides, so there is no way around the pauses.
The way most games gets around this is to keep an array of the current state of all required keys and check periodically on them (for example in the game loop) and act on that (e.g move).
public class KTest extends JFrame implements KeyListener {
private boolean[] keyState = new boolean[256];
public static void main(String[] args) {
new KeyTest();
int xVelocity = 0;
int x = 0;
while(1) {
xVelocity = 0;
if(keyState[KeyEvent.VK_LEFT]) {
xVelocity = -5;
}
x += xVelocity;
}
}
KTest() {
this.addKeyListener(this);
}
void keyPressed(KeyEvent e) {
key_state[e.getKeyCode()] = true;
}
void keyReleased(KeyEvent e) {
key_state[e.getKeyCode()] = false;
}
}
Base class taken from: http://content.gpwiki.org/index.php/Java:Tutorials:Key_States

Arduino mega + GPS module

I'm using an Arduino Mega with a GPS module (PMB-648 GPS), I can see everything that the GPS sends to me:
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPRMC,144547.705,V,5458.6542,N,00136.4148,W,,,240512,,,N*65
$GPGGA,144549.705,5458.6542,N,00136.4148,W,0,00,,20.6,M,47.8,M,,0000*51
This is ok, but now I need to isolate the string that begins with "$GPRMC" and put it into another variable, the string change when the GPS change position, only the "$GPRMC" remains.
this is my code:
String GPSstring ="";
boolean stringComplete = false;
void setup(){
Serial.begin(9600);
Serial2.begin(4800);
}
void loop(){
if (stringComplete){
Serial.println(GPSstring);
GPSstring = "";
stringComplete = false;
}
}
void serialEvent2(){
while(Serial2.available()){
char inchar = (char)Serial2.read();
GPSstring += inchar;
if(inchar == '\n'){
stringComplete = true;
}
}
}
easiest way would be to create a StringObject and use the startsWith() method.
It pays to be lazy. Take a look at the TinyGPS Library for ARduino to parse your NMEA strings easily.