Input from PC keyboard

Hi!

Creating a few small sketches I try to achieve char from the USB connected PC. Using serial monitor for debugging UNO doings work.

Now I tried to pick up a start command from the PC but that simple thing is not working.
What is my mistake?

setup...
  Serial.begin(9600);
.
.
.
The code

  // wait for command to start
  while (Serial.available()== 0) {};
  int dummy = Serial.read();// read command char
.[code]
.
.

Have a look at Robin2's Serial input basics tutorial to see how to reliably read serial data in a non-blocking fashion.

Railroader:
Now I tried to pick up a start command from the PC but that simple thing is not working.
What is my mistake?

So you showed the three lines you know are right and left out any other part of your code that might be the issue and you want someone to tell you what's wrong? Get real. Show code that compiles and shows the problem so someone can actually help you.

Ok. I thought You would see what’s missing so I tried to safe code scrolling.

Here is the complete code:
Sorry but I don’t find the toolbar now to make You the scrolling window at this stage.

//#include

#define fwd HIGH
#define bwd LOW
#define motorX 1
#define motorY 2
#define motorZ 3

void setup()
{
// put your setup code here, to run once:
pinMode(2, OUTPUT); //can be used as x-step
pinMode(3, OUTPUT); //y-step
pinMode(4, OUTPUT); //z-step
pinMode(5, OUTPUT); //can be used as x-dir
pinMode(6, OUTPUT); //y-dir
pinMode(7, OUTPUT); //z-dir
pinMode(8, OUTPUT); //enable active low
pinMode(13, OUTPUT);
digitalWrite(8, LOW);//enable motorpower
Serial.begin(9600);
}

//void stepmotor(int, int);

void stepmotor(int (motor), int(dir), int (dist))
{
if ( dir == fwd )
{
Serial.print("Fwd motor ");
Serial.println(motor);
}
else
{
Serial.print("Bwd motor “);
Serial.println(motor);
}
switch (motor)
{
case motorX:
{
digitalWrite( 5, dir);//digital dir does not owerride analoWrite(5, dir)
delayMicroseconds(100);
break;
}
case motorY:
{
digitalWrite( 6, dir);
delayMicroseconds(100);
break;
}
case motorZ:
{
digitalWrite( 7, dir);
delayMicroseconds(100);
break;
}
}
digitalWrite(8, LOW);//enable motorpower
for (int count = 0; count < dist; count++)
{
if (count == 7999) Serial.println(” 8000 ");
else if (count == 15999) Serial.println("16000 ");
else if (count == 23999) Serial.println("24000 ");
else if (count == 31999) Serial.println("32000 ");
if (motor == motorX )
{
digitalWrite( 2, HIGH);// digital x-step
delayMicroseconds(200);
digitalWrite( 2, LOW);// digital x-step
delayMicroseconds(200);
}
if (motor == motorY)
{
digitalWrite( 3, LOW);
delayMicroseconds(200);
digitalWrite( 3, HIGH);
delayMicroseconds(200
);
}
if (motor == motorZ)
{
digitalWrite( 4, LOW);
delayMicroseconds(200);
digitalWrite( 4, HIGH);
delayMicroseconds(200);
}
}
delay(1000);
digitalWrite(8, HIGH);//enable motorpower
}
void loop() {
// put your main code here, to run repeatedly:
// wait for command to start
while (!Serial.available()) {};
int dummy = Serial.read();// read command char

// analogWrite( 0, Low);//tst
// analogWrite( 1, Low);//tst
// analogWrite( 2, Low);// x-step analog KLAR
// analogWrite( 3, Low);// y-step analog KLAR
// analogWrite( 4, Low);// z-step analog¨KLAR
// analogWrite( 5, Low);// x-dir analog KLAR
// digitalWrite(2, LOW);// x-step digitalt
// digitalWrite(3, LOW);// y-step digitalt
// digitalWrite(4, LOW);// z-step digitalt
// digitalWrite(5,LOW); // x-dir digitalt
// digitalWrite(6,HIGH);// y-dir KLAR HIGH == CW LOW == CCW
// digitalWrite(7,HIGH);// z-dir KLAR HIGH == CW LOW == CCW KLAR
// digitalWrite(8,LOW);//
// digitalWrite(9,LOW);//
// digitalWrite(10,LOW);//
// digitalWrite(11,LOW);//
// digitalWrite(12,LOW);
stepmotor( motorX, fwd, 40000 );
delay(1000);
stepmotor( motorY, fwd, 40000 );
digitalWrite(13, LOW);
delay(1000);

// analogWrite( 0, High);//
// analogWrite( 1, High);//
// analogWrite( 2, High);//x-channel KLAR
// analogWrite( 3, High);//y-channel KLAR
// analogWrite( 4, High);//z-channel KLAR
// analogWrite( 5, High);//x-dir KLAR
// digitalWrite(2, HIGH); //x-step!!!
// digitalWrite(3, HIGH); //y-step!!!
// digitalWrite(4, HIGH); //z-step!!!
// digitalWrite(5, HIGH); //X-DIR CW ON HIGH CCW ON LOW
// digitalWrite(6, HIGH); //Y-DIR CW ON HIGH CCW ON LOW KLAR
// digitalWrite(7, LOW); //Z-DIR CW ON HIGH, CCW ON LOW KLAR
// digitalWrite(8,HIGH);//
// digitalWrite(9,HIGH);//
// digitalWrite(10,HIGH);//
// digitalWrite(11,HIGH);//
// digitalWrite(12,HIGH);//
stepmotor( motorX, bwd, 4000 );// går finfint +200, -200 steg
delay(1000);
stepmotor( motorY, bwd, 4000 );// går finfint +200, -200 steg
digitalWrite(13, HIGH); //
delay(1000);
}

groundFungus:
Have a look at Robin2’s Serial input basics tutorial to see how to reliably read serial data in a non-blocking fashion.

You can't use code tags? Can't read forum rules? Can't cut that down to a complete example that compiles and runs and exhibits the problem?

Man you sure do like to make it hard for someone to help you.

Now you define "not working". I'll posit thatit does exactly what the code says to do. So it works just fine. It may not do what you expect, but it works.

Now, would you mind filling us in on what you expect to have happen and how that compares with what really happens?

I have read the rules for using Forum more than once. Issuing my first, new question I managed to find the code tag and create the code scrolling window. Adding code in my reply, I couldn’t find that code tag. I really tried! Win 10 is used…

In the initial qeustion it says that I wish to send a start character from the PC to the Arduino. Then the loop() will run and at the beginnning of the next loop() the same will happend, wait for a PC char.

Sorry, I have respect for You answering us. I have been the helper for 40 years, in other environments so I have some idea. Just being new to this environment.

What happens when you run the code?

The sketch gets stuck waiting for a char from the Pc. in the very beginning of the loop().

Printing the value of " Serial.available" gives 0 however i press the Pc keyboard.
I try, using debugging Serial.print, to figure out what is missing. I do not manage to send a char from Pc to Arduino. That is the situation.

Just pressing keys on the keyboard doesn't send anything out to serial. Open the serial monitor and type a character in there and hit send.

I have opened the Serial monitor from inside the Arduino sfer and placed the cursor in that window, then typing chars on the Pc keyboard.
My debugging messages show up in the Serial monitor.

Enter your text in the line line in the red box and press Enter or click the send button. That is the only way to send to the serial port with serial monitor. On the lower right, make sure the baud rate matches what is set in the Serial.begin() in the sketch.

serial mon send.jpg

Oops. I typed an answer but befor sending it I scrolled and increased karma for Your help. Then my text got lost…

I found the "command line" in the Serial monitor, typed there, and my sketch runs as I want it to.
Lots of thanks to all of You helping! God bless You, or, I just wish You the very best.