Pages: [1]   Go Down
Author Topic: Continuous serial read  (Read 704 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm currently combining tutorials on motors with serial comms. Im trying to read the the value of the motorPin and println to serial. This seems to be working except for the fact that the value is only read at the end of the loop cycle. How can my code read the value and print to serial  continuously as it changes rather than just at the end of the loop?

Thanks

Code:
//Define Pins
int motorPin = 9;
void setup()
{
  //Set the PWM Motor pin as an output
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
 
}

void loop()
{
 
   //Increase Motor Speed from 0 -> 255
  for (int i=0; i<=255; i++)
  {
    analogWrite(motorPin, i);
    delay(10);
  }
 
  delay(0); //Hold it!
 
  //Decrease Motor Speed from 255 -> 0
  for(int i=255; i>=0; i--)
  {
    analogWrite(motorPin, i);
    delay(10);
  }
 
  delay(0); //Hold it! ;
 
  //print value of motor to serial
 
  int val = analogRead(motorPin) ;
 
  while (Serial.available() ==1) ;
  Serial.println(val) ;
  delay(50) ;
 
 Serial.flush();
 
}

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for(int i=255; i>=0; i--)
  {
    analogWrite(motorPin, i);
    Serial.println (analogRead (motorPin));
    delay(10);
  }
Or, have a look at the Blink Without Delay example to see how to do it without delay.

(Should that be "Or, have a look at the Blink Without Delay example to see how to do it, without delay." ?
Must ask Lynne Truss)
« Last Edit: March 29, 2011, 11:13:39 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the "Blink Without Delay" for an example of how to do scheduled things (like increasing and decreasing motor speed every 10 miliseconds) without putting in delays.  Then you can do serial i/o at whatever interval you like while still making steady changes to the PWM output.

Note:  analogRead() only works on the Analog Input pins, not the PWM Output pins.  If you want to send the value you just sent to the PWM pin you should save that value:

Code:
if ('time to increase the motor speed)
    {
    speed++;
    analogWrite(motorPin, speed);
   }

if (time to send the speed value)
    Serial.println(speed);
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
analogRead() only works on the Analog Input pins, not the PWM Output pins.
oops - missed that biggy!
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lots of logical errors and unneeded statements. However the biggest problem is that you are trying to do a analogRead() on pin 9 ? There are only 6 analog input pins (0-5) and it's not clear what you might have wired to a analog input pin, if anything? Your pin 9 is being used as a PWM output pin to power a motor at variable speed. Reading back that value makes little sense as it's only a digital on/off signal of variable duty cycle that wouldn't signify anything even if you did a digitalRead of that pin.

So before even trying to clean up your sketch, what are you trying to read back in your analogRead() statement? Do you really have an external analog voltage avalible to read into your sketch?

Lefty

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you  smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

managed to get what I was looking and clean it up a little, gonna try the suggestions regarding avoiding the use of delays. Cheers all, really helpful!

Code:
//Define Pins
int motorPin = 9 ;
int i ;

void setup()

{
  //Set the PWM Motor pin as an output
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
 }

void loop()
{
 
   //Increase Motor Speed from 0 -> 255
  for (int i=0; i<=255; i++)
  {
    analogWrite(motorPin, i);
    Serial.println(i) ;
    delay(10) ;
   }
  
  delay(1000); //Hold it!
  
  //Decrease Motor Speed from 255 -> 0
  for(int i=255; i>=0; i--)
  {
    analogWrite(motorPin, i) ;
    Serial.println(i) ;
    delay(10) ;
  }
  
  delay(1000); //Hold it! ;
« Last Edit: March 29, 2011, 12:42:15 pm by acrossell » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
analogRead(i) ;
You don't have 256 analogue inputs.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

haha literately just took that line out too, I'm starting to get it a little bit, sure you have realised this is all pretty new to me. I cant code at all so i'm just ripping tutorials apart and trying to see how it all ticks.
« Last Edit: March 29, 2011, 12:45:05 pm by acrossell » Logged

Pages: [1]   Go Up
Jump to: