arduino-computer communication

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
big93
 
Posts: 56
Joined: Wed Nov 21, 2007 10:31 pm

arduino-computer communication

Post by big93 »

is there a way to communicate between the arduino and the computer, and i dont mean recieveing information and showing it on the serial screen, i mean if lets say i want to press the letter "H" and i want an led to light up, how do i code that, can somebody write up a quick script the lets me turn on an led when i press a key, or something like that, thanks!!
-big92+1

User avatar
frank26080115
 
Posts: 126
Joined: Fri Jun 15, 2007 1:04 am

Post by frank26080115 »

http://processing.org/ <-- easy programming language
http://processing.org/reference/key.html <-- how you will get the key "H"
http://processing.org/reference/librari ... index.html <-- all computer serial commands
http://processing.org/reference/librari ... rite_.html <-- the command to send a byte out to serial

big93
 
Posts: 56
Joined: Wed Nov 21, 2007 10:31 pm

Post by big93 »

thanks a bunch, i'm just heading out for this thing i gotta do, but when i get back 2 nite, ill try all that stuff out!!

just some q's about those good links. First up, whats the site u gave me, is it an easier program then the arduino coder thing?

second, how does sending bites ( or bytes ) to the arduino help me, if i send a byte, what does it do? like do i need a screen to send a byte to? or some other communication type? I know ladyada posted tut's about this, but it's still vague to me how the bytes things work, and what they give ( like what are they good for???)

thanks again!
-big93

User avatar
tomp
 
Posts: 57
Joined: Sun Nov 25, 2007 9:07 pm

Post by tomp »

big93 wrote:just some q's about those good links. First up, whats the site u gave me, is it an easier program then the arduino coder thing?
'Processing' is another programming language & environment, similar to the Arduino programming environment, but the programs run on your computer, not on the Arduino. You need to download and install it separately.

It's not really necessary to use Processing if you just want to play with controlling the Arduino from your computer. The serial monitor in the Arduino console will let you send characters to the board, too, and this is easier if you're just playing around.
big93 wrote:second, how does sending bites ( or bytes ) to the arduino help me, if i send a byte, what does it do? like do i need a screen to send a byte to? or some other communication type? I know ladyada posted tut's about this, but it's still vague to me how the bytes things work, and what they give ( like what are they good for???)
Actually, it doesn't look like this is going to be covered in the tutorial until lesson 8, but it's not that hard.

What you need to do is to read the bytes that are sent over from the computer using Serial.read(). What's sent are the ascii codes for the characters you want to send. So what you do is read a character at a time using Serial.read(), which gives you one ascii code at a time, and compare that with the ascii code for the character that you want to use to control your led.

Here's a sample sketch. It sets pin 10 high or low every time you send over the 'l' character (that's a little 'L').

Code: Select all

/*
 * Turn an led on and off when a special character 
 * is sent over the serial connection.
 */

int ledPin = 10;      // led is hoooked up to this pin
int onOffChar = 108;  // ascii code for the key we'll use to control the led
int ledState;
int nextChar;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  ledState = LOW;
}

void loop(){
  if (Serial.available()) {
    nextChar = Serial.read();
    Serial.println(nextChar);
    if (nextChar == onOffChar) {
      if (ledState == HIGH) {
        ledState = LOW;
        Serial.println("led OFF");
      } else {
        ledState = HIGH;
        Serial.println("led ON");
      }
      digitalWrite(ledPin, ledState);
    }
  }
}
After you load this sketch on your Arduino, use the rightmost icon in the group of five icons at the top of the the Arduino console to turn on the 'Serial Monitor'. There's a white box there where you can type stuff - whenever you hit the 'Send' button (or the 'return' key), whatever you typed is sent over to your arduino board. The sketch above will echo back the ascii codes that are received. Whenever you send an 'l', the led you have hooked up to pin10 will turn on or off. Give it a whirl.

big93
 
Posts: 56
Joined: Wed Nov 21, 2007 10:31 pm

Post by big93 »

wow, thanks for the great detail, thats pretty awsome.

but lets say i want to change the letter from l to p, how do i do that, becuase i could not find an l in the script, so i wasent sure what to change

User avatar
tomp
 
Posts: 57
Joined: Sun Nov 25, 2007 9:07 pm

Post by tomp »

The variable 'onOffChar' is set to 108, which is the ascii code for a little 'L'. This is what you need to change if you want to use a different character. The sketch I gave you will write the ascii code of each character that's received back to the serial port, so you can find the code for any key (even 'p'!) by sending it to the arduino and seeing the code that's echoed back. Try it.

There's also a chart in the arduino reference showing the ascii codes for all the characters. Under the help menu on my arduino console, one of the choices is 'Reference'. If you use that to open the reference page, you should find a link near the bottom that says 'ASCII chart'. Click that to get a page that shows all of the ascii codes. If you're on a Linux or MacOS X machine, you can also just open a terminal window and type 'man 7 ascii'. (You'll want the codes from the 'decimal' table, at the bottom.)

big93
 
Posts: 56
Joined: Wed Nov 21, 2007 10:31 pm

Post by big93 »

thanks alot, now you really seem to know your coding, so i want to ask you another question while i have a good coder to ask!!

lets say i have a series of buttons, or keys i want to turn into a keypad, and make it password accesible. how do i code up a thing that will be checking if the correct keys are being pressed. im sure you know, but i need it to check like "is this key pressed, if so, the next key can be pressed" like that, and then maybe make an led light up if it is the correct code, and maybe a buzzer go off if it is the wrong 4 or 5 button combination.

i'm pretty sure it uses "if" statements, but how do i tell it to ignore the statment if it is typed in incorrectly, or make it give an outcome ( led, or siren ) to a pin?

you dont have to give me a code, you can explain, becuase as i can see, people (no one person in particular;)) don't like writing sample codes sometimes... lol

if ur tired of explainin, u dont gotta reply, i understand

User avatar
tomp
 
Posts: 57
Joined: Sun Nov 25, 2007 9:07 pm

Post by tomp »

lets say i have a series of buttons, or keys i want to turn into a keypad, and make it password accesible. how do i code up a thing that will be checking if the correct keys are being pressed. im sure you know, but i need it to check like "is this key pressed, if so, the next key can be pressed" like that, and then maybe make an led light up if it is the correct code, and maybe a buzzer go off if it is the wrong 4 or 5 button combination.
You could adapt the little sketch I gave you to do something like this by using a variable that counted how many 'good' characters you've gotten so far, and then using this to decide what to compare the next letter to. When you've gotten the complete sequence of letters, then you turn on the led (or whatever); etc.

Here's a working example,

Code: Select all

/*
 * Turn an led on when a special sequence of characters 
 * (a password) is sent over the serial connection.
 */

int ledPin = 10;       // the led is hoooked up to this pin
int passChar1 = 'c';   // the first character of the password
int passChar2 = 'k';   // the second character of the password
int passChar3 = 'r';   // the third character of the password
int passChar4 = '7';   // the fourth character of the password
int nextChar;
int charCount;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  charCount = 0;
}

void loop(){
  if (Serial.available()) {
    nextChar = Serial.read();
    Serial.println(nextChar);
    
    if (charCount == 0) {
      // check for the first char of the pwd...
      if (nextChar == passChar1) {
        charCount = 1;  // got it, so update the character counter
      } else {
        charCount = 0;  // wrong character, so reset count to zero
      }
    }
    else if (charCount == 1) {
      // check for the second char of the pwd...
      if (nextChar == passChar2) {
        charCount = 2; // got it, so update the character counter
      } else {
        charCount = 0; // wrong character, so reset count to zero
      }
    }
    else if (charCount == 2) {
      // etc.
      if (nextChar == passChar3) {
        charCount = 3;
      } else {
        charCount = 0;
      }
    }
    else if (charCount == 3) {
      if (nextChar == passChar4) {
        charCount = 4;
      } else {
        charCount = 0;
      }
    }
    
    // has complete password been entered yet?
    if (charCount == 4) {
        Serial.println("password accepted");
        // led on
        digitalWrite(ledPin, HIGH);
        charCount = 0;
     }
     else if (charCount == 0) {
       // count was set to zero, meaning a bad char was received.
        Serial.println("Bzzt!  bad password");
        // blink the led
        digitalWrite(ledPin, HIGH);
        delay(100); 
        digitalWrite(ledPin, LOW);
        delay(100);
    }
  }
}
Try it and experiment a little. Do you think this is very secure? (If not, why not?)

Finally, I should say that there are much cleaner ways of doing this using strings or arrays, but you should be comfortable with code like this, first.

big93
 
Posts: 56
Joined: Wed Nov 21, 2007 10:31 pm

Post by big93 »

very nice, thank you for the experiment.

as for your quiz questions, it doesent seem like a secure method, becuase over a long period of time it can be cracked, by sending indavidual letters to the serial port, and seeing if it is the correct letter, ex:

:::::::sends letter r:::::::: and comes back incorrect, next tries the letter c, and look at that, it's correct! so i have the first letter of the password, and so on and so fourth until the password is found!

and i understand the code, which is a good thing, becuase i know all i need to do to use, lets say buttons, is i just need to replace the charecters, with a switch pin, and i have my input as a switch, right?

o yea, and 1 more q, if i made this use tact buttons connected to the arduino, can i disconnect the arduino from the computer and use it without a serial input into the computer, or is this code based on an arduino connected to a computer only, and doese not support battery use?

User avatar
tomp
 
Posts: 57
Joined: Sun Nov 25, 2007 9:07 pm

Post by tomp »

big93 wrote:as for your quiz questions, it doesent seem like a secure method, becuase over a long period of time it can be cracked, by sending indavidual letters to the serial port, and seeing if it is the correct letter, ex:

:::::::sends letter r:::::::: and comes back incorrect, next tries the letter c, and look at that, it's correct! so i have the first letter of the password, and so on and so fourth until the password is found!
That's right! That means it would take someone no more than four times the number of possible characters to figure out a four-character password, and on average it would take only half that many attempts. If you let people use the upper and lower case letters and the digits, it would take at most 248 attempts to figure out any password by trial and error. That's not even minimally secure. If you forced the user to enter the whole four letter password before telling them whether it was correct or not, it makes it way harder to guess by trial and error, because then they'd have to try all of the roughly 14 billion possible four-character passwords, one by one.
and i understand the code, which is a good thing, becuase i know all i need to do to use, lets say buttons, is i just need to replace the charecters, with a switch pin, and i have my input as a switch, right?
I'm not sure what you're suggesting. It seems like using a bunch of switches to specify a password instead of characters would make it much easier to guess the password. It would be like only letting the password consist of ones and zeros. There are only 16 possible ways to set four switches, for instance.
o yea, and 1 more q, if i made this use tact buttons connected to the arduino, can i disconnect the arduino from the computer and use it without a serial input into the computer, or is this code based on an arduino connected to a computer only, and doese not support battery use?
The code I gave you assumes the password is coming over the serial line character by character, so it pretty much only works if you're connected to a computer. If you want to use it apart from the computer, you'll have to find another input device for specifying the password. A keypad seems like a natural thing to use, but I have no idea how to hook one up to the arduino. (I'm a software guy - here to learn electronics.) Maybe you could get some ideas if you poked around on the arduino playground site?

big93
 
Posts: 56
Joined: Wed Nov 21, 2007 10:31 pm

Post by big93 »

yea, that sounds true, ill post sumthin at the arduino site, i'm also trying to alter the code to accept the tact switches, just as a beta to any real thing i would possible try. And i know it will be way too easy, but its a beta, so its no biggy, i'm gonna use 3 tact buttons, and make it so that they have to be pressed in a certain order, so the light will stay on, but its obviously not worth ever using for real, becuase if you pressed the wrong button, the led tells you your wrong, so you dont have to use any more then 9 tries to brute force it... but what the hey.

o yea, and if u want to know how to hook up a keypad, look in the arduino site on tut's or around the arduino site, becuase i know for a fact i saw sumthin on how to connect a keypad, somewhere on there.
thnks

p.s thanks double, becuase i was trying to learn how to use if statements, and i learned both password like stuff, and how to use if statements!

mako
 
Posts: 1
Joined: Tue Oct 27, 2009 12:43 am

Re: arduino-computer communication

Post by mako »

HI
first of all tnx ;)
here is some modified code for making a keyboard 4x4 to work with the password code,
/*
* Turn an led on when a special sequence of characters
* (a password) is sent over the serial connection. 
*/

#include <Keypad.h>

char ledpin = 13; // the led is hoooked up to this pin
char passChar1 = '2'; // the first character of the password
char passChar2 = '3'; // the second character of the password
char passChar3 = '3'; // the third character of the password
char passChar4 = '4'; // the fourth character of the password
char nextChar;
char charCount;

//en keypad.h hay... column: output, row: input

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 7, 6, 5, 4 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10, 8 };


// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup() {
  Serial.begin(9600);
  pinMode(ledpin, OUTPUT);
  digitalWrite(ledpin, LOW);
  charCount = 0;
}

void loop(){
  revisa();
      
  
}

void revisa()
{
  char key = kpd.getKey();
   if(key) // same as if(key != NO_KEY)
  {

  
    
    nextChar = key;
    //Serial.println(nextChar);
   Serial.print("*");
    if (charCount == 0) {
      // check for the first char of the pwd...
      if (nextChar == passChar1) {
        charCount = 1;  // got it, so update the character counter
      } else {
        charCount = 0;  // wrong character, so reset count to zero
      }
    }
    else if (charCount == 1) {
      // check for the second char of the pwd...
      if (nextChar == passChar2) {
        charCount = 2; // got it, so update the character counter
      } else {
        charCount = 0; // wrong character, so reset count to zero
      }
    }
    else if (charCount == 2) {
      // etc.
      if (nextChar == passChar3) {
        charCount = 3;
      } else {
        charCount = 0;
      }
    }
    else if (charCount == 3) {
      if (nextChar == passChar4) {
        charCount = 4;
      } else {
        charCount = 0;
      }
    }
   
    // has complete password been entered yet?
    if (charCount == 4) {
        Serial.println("password accepted");
        // led on
        digitalWrite(ledpin, HIGH);
        charCount = 0;
     }
     else if (charCount == 0) {
       // count was set to zero, meaning a bad char was received.
        Serial.println("Bzzt! bad password");
        //Serial.print("*");
        // blink the led
        digitalWrite(ledpin, HIGH);
        delay(100);
        digitalWrite(ledpin, LOW);
        delay(100);
    }
  }
}


im gonna use it to open a door !!!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Starter Pack”