I have a Arduino Mega 2560.
Connected on virtual com4.
My program is sending the same data to serial and serial1.
When i run my program, the internal Arduino Serial Monitor shows tekst "Button".
The hardware RS232 com. port is connect on the correct settings (9600 8N1) and not working.
I only want to read so i only use the tx0 and tx1 pins.
On both pins my PuTTY terminal program under Windows7 show the same carbage.
It is receiving data but it is not readable.
After reading a lot, i thought i need a level changer. but after building a shifter from 5 to 12 volt and doing a last check to activate my max232. I ran into the max voltage of 5v for the MAX232. Because it is used in a lot of arduino serial examples. my already 5v output of the arduino must be good. (so didnt use the MAX232)
the data pussy is receiving is "???Q*?{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??{!#??
"
when i change putty to 'all session output' the putty.log shows : "PuTTY log 2014.10.01 14:50:21 =~=~=~=~=~=~=~=~=~=~=~=
¯ÑÑQë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë ¯ÑÑQë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë{!#åë "
To be honest: I don't care what you read, because you may not connect a hardware serial port to an Arduino.
The official RS-232 is an inverted signal of -3...-12V and +3...+12V. Yes, indeed "inverted".
The MAX232 or MAX2323 is able to generate those voltages itself. It generates that higher voltage (positive and negative) with the 5V. You can build your own converter with the MAX232 or buy a module.
Perhaps it is easier to buy a usb-ttl-serial adapter.
At this moment, you Arduino board might already be damaged. You can try using another TX pin with the MAX232(3).
You can use any serial terminal program on the computer for the serial-via-usb of the Mega board. Why do you want to use another serial port ?
first thanks for the insight on the working of the max232 -> that it is possible (with a working voltage of 5V) to generate and work with 12V RS232 signals. I expected with a working voltage of 5V to have a max of 5V output.
I will put the MAX232 between my project.
Second I was lucky to be only interested in the output of the arduino (and not feedback from the RS232 to the arduino) and for that reason only connected the line from the arduino to the RS232. So i never send 12V to the arduino.
I use an arduino as test project. I have a tiny25 that i want to use to send me serial signals to my RS232 as a way of monitoring.
The tiny25 is more limited then the arduino.
So on the arduino it looks crazy to work like this but with my goal in mind it is the way to get there.
On my tiny25 i am going to receive a analog signal that will spread over time. (bounds in a 1,5 second timelap)
On my laptop i want to get a bitstream of values that are samples of the analog input and send it as numbers to my laptop.
That way i can map the behavior and adapt my program to the input I get.
Arduino is a great platform to test my program before sending it permanently to the tiny25.
The program is simpel.
Send string "value" to both serial ports (TX0 and TX1) read TX0 with the arduino serial monitor (over usb cable) and read TX1 on com1.
Gave me a lot of frustrating days.....
Thanks for all the information for getting this project on the road.
M.
//latest test programma (before integrating the MAX232)
//because between the sample there is some "white space" i am counting the zero samples to make sure that my reading doesn't keep on going
//as a test i am sending some in between strings. to keep track on the status of the reading. (to much white space)
//program is only ready to read a sample series after the button is pressed
//zerocount is set to 1400 because the int will never have a bigger value then 1500 (don't know why)
const int sensorPin = A0;
const int buttonPin = 2;
const int LedPin = 7;
int active = 0;
int sensorValue = 0;
int buttonstate = 0;
int zerocount = 0;
int sampleCount = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(LedPin, OUTPUT);
Serial.begin(9600);
Serial.println("Running: SlagGevoeligheid programma");
}
void loop() {
buttonstate = digitalRead(buttonPin);
if (buttonstate == HIGH){
digitalWrite(LedPin, HIGH);
}
else{
digitalWrite(LedPin, LOW);
}
sensorValue = analogRead(sensorPin);
//we only want the stream to begin when the first number hits.
if (sensorValue > 500 and !active) {
Serial.println("Reading Started");
active = true;
sampleCount = 0;
digitalWrite(LedPin, HIGH);
}
while (active){
sensorValue = analogRead(sensorPin);
if (zerocount > 1400){
active = false;
digitalWrite(LedPin, LOW);
zerocount = 0;
Serial.println("Reading Ended");
String samplestring = "samples:"+String(sampleCount);
Serial.println(samplestring);
break;
}
//make shure the number of zeros is not getting to big.
if (sensorValue = 0) {zerocount = zerocount + 1;}
sampleCount = sampleCount + 1;
if (zerocount = 500) {Serial.println("500");}
if (zerocount = 1000) {Serial.println("1000");}
if (zerocount = 1500) {Serial.println("1500");}
String sensorstring = "value:"+String(sensorValue);
Serial.println(sensorstring);
Serial1.println(sensorstring);
String zerostring = "zeros:"+String(zerocount);
Serial.println(zerostring);
}
}
ATtiny25 is very small : 2k flash, 128 byte ram.
The ATtiny85 is more Arduino compatible, the Trinket uses it.
I did some tests, but I commented out the line with "Serial1", and I used the ATmega8 to compile, with Arduino IDE 1.5.8 and -flto option.
Start with : 4664 byte code, 310 byte ram.
Using 'F()' macro with Serial.println : 4670 bytes code, 234 byte ram (less ram, more code)
Make 'active' a boolean, and 'buttonstate' a byte : 4634 bytes code, 232 bytes ram.
Replaced three String with separate Serial.print : 2968 bytes code, 198 bytes ram
Combined button to led code : 2962 bytes code, 197 bytes ram.
Replaced string with variable 'zerocount' : 2948 bytes code, 197 bytes ram.
Corrected the '=' with '==' in if-statement, and combined printing zerocount: 2934 bytes code, 197 bytes ram.
The code size can be reduced to 2896 when you use two characters for each string, for example instead of "zeros:" the shorter "z=".
I hope this helps to make your sketches smaller. The String class uses a lot of memory, I think you can not use it in an ATtiny25.
Below is the sketch with optimizations:
const int sensorPin = A0;
const int buttonPin = 2;
const int LedPin = 7;
boolean active = false; // false
int sensorValue = 0;
// byte buttonstate = 0;
int zerocount = 0;
int sampleCount = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(LedPin, OUTPUT);
Serial.begin(9600);
Serial.println(F("Running: SlagGevoeligheid programma"));
}
void loop() {
// buttonstate = digitalRead(buttonPin);
// if (buttonstate == HIGH){
// digitalWrite(LedPin, HIGH);
// }
// else{
// digitalWrite(LedPin, LOW);
// }
digitalWrite( LedPin, digitalRead(buttonPin));
sensorValue = analogRead(sensorPin);
//we only want the stream to begin when the first number hits.
if (sensorValue > 500 and !active) {
Serial.println(F("Reading Started"));
active = true;
sampleCount = 0;
digitalWrite(LedPin, HIGH);
}
while (active){
sensorValue = analogRead(sensorPin);
if (zerocount > 1400){
active = false;
digitalWrite(LedPin, LOW);
zerocount = 0;
Serial.println(F("Reading Ended"));
// String samplestring = "samples:"+String(sampleCount);
Serial.print(F("samples:"));
Serial.println(sampleCount);
break;
}
//make shure the number of zeros is not getting to big.
if (sensorValue == 0) {
zerocount++;
}
sampleCount++;
if (zerocount == 500 || zerocount == 1000 || zerocount == 1500 ) {
Serial.println(zerocount);
}
// String sensorstring = "value:"+String(sensorValue);
Serial.print(F("value:"));
Serial.println(sensorValue);
// Serial1.println(sensorstring); ???????????
// String zerostring = "zeros:"+String(zerocount);
Serial.print(F("zeros:"));
Serial.println(zerocount);
}
}