Newbie question: Can I interface with comp

Sorry this may sound like a stupid question but i want to know if i can program my board so that it runs a function if i press a key eg "up" and runs another function if i press another key.

Many thanks

i want to know if i can program my board so that it runs a function if i press a key eg "up" and runs another function if i press another key.

Yes, you have our permission to.

If I was arguing semantics I would say it was not permission that was being sought but asking if he had the capability to write the program. To which the answer is no not at the moment.

However you could learn how to do it and it is well within the capability of an arduino to do that sort of thing.

Sorry about the leg pulling and welcome to the wonderful world of Arduino.

She actually! Yeah i wasn't sure i had the capabilities, I wonder if you have any advice on where to look to gain that kind of knowledge. I've currently written functions to drive solenoids, and to run a heater.

Any programming environment on the PC that can acces a serial port can be used to write the PC side program.

If you have experience with one, use that, otherwise a good place to start is the free language Processing.

http://processing.org/

Processing is kind of a sister language to Arduino. Well actually the Arduino IDE is based on Processing.

The Arduino program to read the data from the PC is not very complicated to write, you can find lots of examples on similar programs by lookin around this forum.

Don't hesitate to ask Arduini questions here. There's a Processing forum at the Processing site too.

Go for it.

Dear EMS...

It really isn't hard... as long as you really do want something simple.

While the program involves LOTS more than what you want, everything you need is in the program that goes with...

http://sheepdogguides.com/arduino/ar3ne1sd.htm

I'll try to do a tutorial to show you the essentials, but that will take some time. Have a look at the above in the meantime.

Here’s the code…

N.B.: You have to start the serial monitor…

http://sheepdogguides.com/arduino/aht1serimoni.htm

… after uploading the program, you need an LED on pin 13, and you enter “s” or “q” on the PC to change how fast the LED flashes. You HAVE TO PRESS THE “enter” KEY after each s or q.

//PCtoArd
//ver 26Jly10

//#include <avr/pgmspace.h>
//#include <sensor_pff.h>

//SENSOR_PFF sd;
//byte sensor_start = 0;

#define LEDpin 13 //no ; here
char buff[128];      /* i/o buffer, for get_line */
boolean boFlashQuickly=false;

void setup()
{
        pinMode(LEDpin,OUTPUT);
        delay(300);//let system settle
        Serial.begin(9600);
      
        Serial.println("\n\nDemo of control of Arduino from PC\n");      
      Serial.println("See http://sheepdogguides.com/ardino/ahttoc.htm\n");
        Serial.println("for latest details.\n");      
        Serial.println("\nPress the enter key after pressing s or q.\n");      
      Serial.print('>');
}

void loop()
{
  char *ptr;
      
  if(Serial.available()==true){//Cmnds waiting
    
       get_line(buff, sizeof(buff));
       ptr = buff;
      
       switch (*ptr++) {//switch
       
       case 'q':   // Quickly flash
          //Serial.print("\nWill flash quickly\n");
          boFlashQuickly=true;
          break;//end "q"

       case 's':    // Slowly flash
          //Serial.print("\nWill flash slowly\n");
          boFlashQuickly=false;
          break;//end "q"

       default:
          Serial.print("Unrecognized. Only s and q recognized\n");
          break;//end default
                  
      }//end "switch..."
    Serial.print('>');
   }//if, cmnd waiting
  else {//1 no cmnds waiting
   if (boFlashQuickly){//2
        QuicklyFlash();
        }//end if "2"
      else {//2
        SlowlyFlash();
        }//end of else(2)        
    }//1 end of level 1 "if" clause, no cmnds waiting
}//end "loop()"

void QuicklyFlash(){
    LEDOn();
    delay(100);
    LEDOff();
    delay(100);
}

void SlowlyFlash(){
    LEDOn();
    delay(900);
    LEDOff();
    delay(900);
}

void LEDOn(){
digitalWrite(LEDpin,LOW);//Yes- LOW for ON  
};

void LEDOff(){
digitalWrite(LEDpin,HIGH);  
};

//Treat "GET_LINE" as a black box, for now.
//Nothing to change in it.
//Press the "enter" key after entering your 1 letter command
static void get_line (char *buff, byte len)
{
      byte c;
      int idx = 0;

      for (;;) {//1 Start infinite loop... will only exit via "break"
            if(Serial.available()){//2
                  c = Serial.read();
                  if (((byte)c >= ' ') && (idx < len - 1)) {
                        buff[idx++] = c; Serial.print(c);
                  }//3
            }else break;//2
      }//1 (ends "for...")
      buff[idx] = 0;
//      Serial.print(c);
      Serial.print('\n');
}
//  === END OF GET_LINE ===

I don't mean to hijack the thread but using the technique outlined above, does it mean I can make the arduino think it's talking to a GSM modem instead of a terminal?

Yes that's one of the great things about embedded controllers, you can make them look like anything you like. All you need is the right software. In fact that is how the GSM is working in the first place.

Sigh. While looking for a place in my web pages to incorporate the code I presented earlier, I discovered I'd written something very siliar previously. It is at...

http://sheepdogguides.com/arduino/art4SeSimpFrmIDE.htm

... whre you will find a more detailled explanation and some code which is better in some ways, worse in some ways.

Thank you tkbyd, I have my solenoids working so they turn on and off at the press of a button. :)

would it be possible to add a third variable, I have solenoids on, and solenoids off, how would i add a third option ie a cycle.

Also if the coding is

 digitalWrite(s1, HIGH);
  digitalWrite(s2, HIGH);

How long is the delay between s1 turning on and s2? is it possible to set simultaneous switching on? Obviously not a big deal for the solenoids but for other aspects a simultaneous switch on is useful.

would it be possible to add a third variable, I have solenoids on, and solenoids off, how would i add a third option ie a cycle.

A cycle is a series of off and on, isn’t it? Turn the solenoid on for a while, then turn it off for a while. Lather, rinse, repeat.

The digitalWrite function is not terribly fast. There are a number of clock cycles involved. What the digitalWrite function does is to determine which port and which pin on that port correspond to a given digital pin. Then, it sets or clears the appropriate bit, and alters a number of pins at once. You could do the same, using Direct Port Manipulation.

A cycle is a series of off and on, isn’t it? Turn the solenoid on for a while, then turn it off for a while. Lather, rinse, repeat.

yeah i can do that by pressing y then n and repeating but i was wondering if i could insert another element into the code, so when i pressed g for example it would start a cycle.

//PCtoArd
//ver 26Jly10

//#include <avr/pgmspace.h>
//#include <sensor_pff.h>

//SENSOR_PFF sd;
//byte sensor_start = 0;

#define LEDpin 13 //no ; here
char buff[128];      /* i/o buffer, for get_line */
boolean sON=false;


void setup(){
   int s1= 51;
   int s2= 52;
  pinMode (s1, OUTPUT);
  pinMode (s2, OUTPUT);

       
        Serial.begin(9600);

        Serial.println("\n\nControlling solenoids from pc\n");
      Serial.println("Press y to turn soelnoids on\n");
        Serial.println("press n key to turn them off\n");
        Serial.println("\nPress the enter key after pressing y or n.\n");
      Serial.print('>');
}

void loop()
{
  char *ptr;

  if(Serial.available()==true){//Cmnds waiting

       get_line(buff, sizeof(buff));
       ptr = buff;

       switch (*ptr++) {//switch

       case 'y':   // Turn solenoids on
          
          sON= true;
          break;//end "q"

       case 'n':    // turn solenoid off
          
         sON=false;
          break;//end "q"


       default:
          Serial.print("Unrecognized. Only y, c and n recognized\n");
          break;//end default

      }//end "switch..."
    Serial.print('>');
   }//if, cmnd waiting
  else {//1 no cmnds waiting
   if (sON){//2
       solenoidon ();
        }//end if "2"
      else {//2
        solenoidoff ();
        }//end of else(2)
    }//1 end of level 1 "if" clause, no cmnds waiting
}//end "loop()"

void solenoidon (){
  int s1= 51;
   int s2= 52;
  pinMode (s1, OUTPUT);
  pinMode (s2, OUTPUT);
  digitalWrite(s1, HIGH);
  digitalWrite(s2, HIGH); 
}

void solenoidoff (){
  int s1= 51;
   int s2= 52;
  pinMode (s1, OUTPUT);
  pinMode (s2, OUTPUT);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW); 

}



//Treat "GET_LINE" as a black box, for now.
//Nothing to change in it.
//Press the "enter" key after entering your 1 letter command
static void get_line (char *buff, byte len)
{
      byte c;
      int idx = 0;

      for (;;) {//1 Start infinite loop... will only exit via "break"
            if(Serial.available()){//2
                  c = Serial.read();
                  if (((byte)c >= ' ') && (idx < len - 1)) {
                        buff[idx++] = c; Serial.print(c);
                  }//3
            }else break;//2
      }//1 (ends "for...")
      buff[idx] = 0;
//      Serial.print(c);
      Serial.print('\n');
}
//  === END OF GET_LINE ===

also you say [edit]Then, it sets or clears the appropriate bit, and alters a number of pins at once[/edit] does that imply simultaneous switch on after a short delay or a delay between each pin?

if(Serial.available()==true){//Cmnds waiting

This is wrong. Serial.available() returns the number of bytes available to be read, not a boolean that indicates whether or not there are bytes to read.

but i was wondering if i could insert another element into the code, so when i pressed g for example it would start a cycle.

You can do that. Look at the Blink Without Delay example. Create a variable to hold the last switched time, and whether that was to turn the solenoid on or off. Create a variable to hold whether the solenoid is cycling or not.

On each pass through loop, check if cycling is happening. If so, see if it is time to change the state of the solenoid. If it is, change the state, write the new state, and set the last state changed time.

does that imply simultaneous switch on after a short delay or a delay between each pin?

All the pins at the same time. (Or too close for you to measure the elapsed time between one on and another one on.)

I can write a program to to create a cycle, but i don't know how to insert that into the program, when I try to insert the idea in the code i'm running at the moment, i can't get it io be started up by a serial input like "r".

when I try to insert the idea in the code i'm running at the moment, i can't get it io be started up by a serial input like "r".

Can you lean a little to the left? Your blocking the screen. A little farther... Careful you don't fall out of the chair.

Hmm, still can't see your code.

if(Serial.available()==true){//Cmnds waiting

This is wrong. Serial.available() returns the number of bytes available to be read, not a boolean that indicates whether or not there are bytes to read.

Yes and no. It IS certainly sloppy.

Zero counts as "false" in the context above, so the code "works"

if((Serial.available()>0)==true){//Cmnds waiting

... would be more rigorous.

would be more rigorous

...though somewhat tautological.

Can you lean a little to the left? Your blocking the screen. A little farther... Careful you don't fall out of the chair.

Hmm, still can't see your code.

I believe i posted it on the previous page, reply number 13, and i want to insert a thirt variable so i have y, n and r where r will preform.

void loop()
{
  digitalWrite(s1, HIGH);
 digitalWrite(s2, HIGH); 
  delay(100);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW); 
  delay(100);
}

or something really similar.