Pages: [1]   Go Down
Author Topic: NEED HELP ON PROGRAMING  (Read 1032 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HI
Below is my transmitter code. Can you rectifier it because when i conected a push button to pin 3 and push button is pressed, the data is not transmitting as i observed from the serial monitor(nothing pop from the serial monitor)

int crankingPin    = 3;
int liftoffPin     = 4;
int upwardPin      = 5;
int downwardPin    = 6;
int turnleftPin    = 7;
int turnrightPin   = 8;
int forwardPin     = 9;
int backwardPin    = 10;
int buttonstate;


void setup()
{
// initialize the serial communication:
Serial.begin(9600);

// initialize the ledPin as an output:
  
pinMode(crankingPin, INPUT);
pinMode(liftoffPin, INPUT);
pinMode(upwardPin, INPUT);
pinMode(downwardPin, INPUT);
pinMode(turnleftPin, INPUT);
pinMode(turnrightPin, INPUT);
pinMode(forwardPin, INPUT);
pinMode(backwardPin, INPUT);
}

void loop()
{
  if(digitalRead(crankingPin)==HIGH)
  {
  Serial.write( 8 ) ;
  delay(1000);
  }
 else
 {
   buttonstate = 0;
 }
}


« Last Edit: February 24, 2011, 06:12:39 pm by rooney04 » Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And how is crankingPin wired?
Logged

Per Arduino ad Astra

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50188
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial.write() sends binary data. I personally wouldn't expect to be able to make sense of what is sends to the serial monitor.

With smiley faces in the code, it's really hard to tell what you are sending to the serial port.

I truly hope you don't expect the smiley face to appear in the serial monitor.

For future postings, select the triangle in front of Additional Options below this window, and select the "Don"t use smileys" option.

Sure be nice if similey strings between code tags were ignored.
Logged

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

hi
sorry i m new this forum.

After some example i go through, i change Serial.write( 8 ) ; to Serial.write('8') ; and the serial monitor showing integer 8 when i pressed the push button. So am i transferring data ?
Logged

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

Serial.write() sends binary data. I personally wouldn't expect to be able to make sense of what is sends to the serial monitor.
please refer back to the code. it will be more clear now.
Logged

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

hi
sorry i m new this forum.

After some example i go through, i change Serial.write( 8 ) ; to Serial.write('8') ; and the serial monitor showing integer 8 when i pressed the push button. So am i transferring data ?

Read careful the example options avalible below in serial printing for how the data will be handled and sent to the PC.

http://arduino.cc/en/Serial/Print
Logged

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

yeah i read that, but what you trying to say toward my situation or the problem i face.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50188
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What we are wondering is why you insist on using Serial.write to send ascii data, when that is not what is was meant to send. Why won't you change it to Serial.print()?
Logged

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

Hmm. Basically i need to transfer few 4 bit binary data (1000, 1001, 1011, 1101, 1110 and etc)) to another arduino and the receiving arduino will receive it. For each 4 bit binary data received , it executes a certain routines such controlling the speed of motor or brightness of led, So how can i do that?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50188
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you trying to transfer data to another Arduino via the Serial Monitor? You might use Serial.write() to talk to a device that understands binary data, but you use Serial.print() to talk to the Serial Monitor.

Quote
For each 4 bit binary data received , it executes a certain routines such controlling the speed of motor or brightness of led,
Serial data is one byte/8 bits long. You can not send just 4 bits. What is the significance of sending a binary value, anyway? The receiver still has to compare the received value against all possible values.

Code:
int val = Serial.read();
switch(val)
{
   case 0b0000: Do0();  break;
   case 0b0001: Do1();  break;
   case 0b0010: Do2();  break;
   case 0b0011: Do3();  break;
   case 0b0400: Do4();  break;
}
will be no faster than:
Code:
int val = Serial.read();
switch(val)
{
   case '0': Do0();  break;
   case '1': Do1();  break;
   case '2': Do2();  break;
   case '3': Do3();  break;
   case '4': Do4();  break;
}
The second set of values looks reasonable in the Serial Monitor, too. The first set of values does not.

The character data, since it is one byte long, transmits in the same length of time as the byte value.
Logged

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

yeah i m trying to send binary data to another arduino. So to check i m using serial monitor whether it able to send the binary data or not? basically the code for transmitter can be use right? the receiver code follows your idea?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50188
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I give up on trying to explain why. I'll put it simply:
Use Serial.print() to send data, and Serial.read() to read it.

At some time in the future, you might encounter a situation where Serial.print() is not appropriate, but this is not one of those cases.
Logged

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

i m very for lack knowledge in programming. As for conclusion just "serial.print" to transmit the data to another arduino?
Logged

Pages: [1]   Go Up
Jump to: