Go Down

Topic: Keyboard question (Read 924 times) previous topic - next topic

perkunas

OK I think i figured out how to control a relay via a laptop keyboard
my question is there a list of keys some place like example "if(incomingbyte == 98){"
98 that's the b key, what are the others?.

Code: [Select]
int incomingbyte;
void setup() {
Serial.begin(9600);
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
}
void loop() {

if (Serial.available() > 0) {
incomingbyte = Serial.read();
}
if(incomingbyte == 98){
digitalWrite(7, LOW);
delay(1000);
digitalWrite(7, HIGH);

}

}


frank26080115

I think you need the ASCII table http://www.asciitable.com/
Freelance engineer, consultant, contractor. Graduated from UW in 2013.


WizenedEE

ASCII characters with SINGLE quotes (apostrophes) around them translate into the correct numbers.

Code: [Select]

/*There are all True*/
'b' == 98;
' ' == 32;
'A' == 65;

perkunas

New problem, seems like it all works, but every time I open the Terminal to enter in the key my relay shuts off for like a second as it connects to the port. Here's my program


Code: [Select]
  //  Some macros for defining time intervals in milliseconds
#define seconds_in_ms(s) ((s)*1000UL)
#define minutes_in_ms(m) ((m)*60UL*1000UL)
#define hours_in_ms(h)   ((h)*60UL*60UL*1000UL)
#define days_in_ms(d)    ((d)*24UL*60UL*60UL*1000UL)
#define weeks_in_ms(w)   ((w)*7UL*24UL*60UL*60UL*1000UL)

unsigned long CycleStartTime = 0;
unsigned long LastTaskTime = 0;
unsigned long CurrentTaskInterval = 0;
unsigned Task = 0;
int incomingbyte;
void setup()
{
  Serial.begin(9600);
   pinMode(2, OUTPUT);     //PH down
   pinMode(3, OUTPUT);  //B nutrient solution
   pinMode(4, OUTPUT);  //A nutrient solution
   pinMode(5, OUTPUT);  //Drain pump
   pinMode(6, OUTPUT);   //Solenoid Valve
   pinMode(7, OUTPUT);  //Nutrient Heater
}
   digitalWrite(2, HIGH);     //PH down
  digitalWrite (3, HIGH);  //B nutrient solution
  digitalWrite (4, HIGH);  //A nutrient solution
  digitalWrite (5, HIGH);  //Drain pump
  digitalWrite (6, HIGH);   //Solenoid Valve
  digitalWrite(7, HIGH);  //Nutrient Heater
{

if (Serial.available() > 0) {
incomingbyte = Serial.read();
}
if(incomingbyte == 112){
digitalWrite(2, LOW);
delay(1000);
digitalWrite(2, HIGH);
}

  unsigned long currentTime = millis();

  // If the time has not yet come to perform a task
  if (currentTime - LastTaskTime < CurrentTaskInterval)
    return;   // Nothing to do

  LastTaskTime = currentTime;

  switch (Task++)
  {
  case 0:
    CycleStartTime = currentTime;  // Remember this time

   digitalWrite(5,LOW);   // set the drain pump on
   
    CurrentTaskInterval = minutes_in_ms(10);
    break;


  case 1:
   digitalWrite(6, LOW); // set the Solenoid Valve on
    digitalWrite(2, LOW);  // set the PH down on
    digitalWrite(3, LOW); // set the B nutrient solution on
    digitalWrite(4, LOW); // set the A nutrient solution on
    CurrentTaskInterval = seconds_in_ms(1);
break;

case 2:
   digitalWrite(6, LOW); // set the Solenoid Valve on
   digitalWrite(3, LOW); // set the B nutrient solution on
    digitalWrite(4, LOW); // set the A nutrient solution on
    CurrentTaskInterval = seconds_in_ms(2);
break;


Case 3:
digitalWrite(6, LOW); // set the Solenoid Valve on
CurrentTaskInterval = seconds_in_ms(7);
break;

Case 4:
digitalWrite(7, HIGH); // set the Nutrient Heater on
// Start over again in one week from the START of the cycle
    Task = 0;
    LastTaskTime = CycleStartTime;
    CurrentTaskInterval = weeks_in_ms(1);
    break;
  }
}

WizenedEE

You're going to have to disable auto-reset -- there's a trace you can cut on the back of the board, or a very specific valued resistor can go somewhere (google it).

perkunas

Thanks I'm going to try the resistor capacitor thing what a pain.
I tweaked my sketch a little and now it wont load, I cant see where I went wrong.
a lil help would be really nice
Thanks you guys are the best

Code: [Select]
  //  Some macros for defining time intervals in milliseconds
#define seconds_in_ms(s) ((s)*1000UL)
#define minutes_in_ms(m) ((m)*60UL*1000UL)
#define hours_in_ms(h)   ((h)*60UL*60UL*1000UL)
#define days_in_ms(d)    ((d)*24UL*60UL*60UL*1000UL)
#define weeks_in_ms(w)   ((w)*7UL*24UL*60UL*60UL*1000UL)

unsigned long CycleStartTime = 0;
unsigned long LastTaskTime = 0;
unsigned long CurrentTaskInterval = 0;
unsigned Task = 0;
int incomingbyte;
void setup()
{
  Serial.begin(9600);
   pinMode(2, OUTPUT);     //PH down
   pinMode(3, OUTPUT);  //B nutrient solution
   pinMode(4, OUTPUT);  //A nutrient solution
   pinMode(5, OUTPUT);  //Drain pump
   pinMode(6, OUTPUT);   //Solenoid Valve
   pinMode(7, OUTPUT);  //Nutrient Heater
}
   digitalWrite(2, HIGH); //PH down off
  digitalWrite(3, HIGH);  //B nutrient solution off
  digitalWrite(4, HIGH);  //A nutrient solution off
  digitalWrite(5, HIGH);  //Drain pump off
  digitalWrite(6, HIGH);   //Solenoid Valve off
  digitalWrite(7, HIGH);  //Nutrient Heater off
{

if (Serial.available() > 0) {
incomingbyte = Serial.read();
}
if(incomingbyte == 112){
digitalWrite(2, LOW);
delay(1000);
digitalWrite(2, HIGH);
}
if(incomingbyte == 110){
digitalWrite(3, LOW);
digitalWrite(4,LOW);
delay(1000);
digitalWrite(3, HIGH);
digitalWrite(4,HIGH);
}

  unsigned long currentTime = millis();

  // If the time has not yet come to perform a task
  if (currentTime - LastTaskTime < CurrentTaskInterval)
    return;   // Nothing to do

  LastTaskTime = currentTime;

  switch (Task++)
  {
  case 0:
    CycleStartTime = currentTime;  // Remember this time

   digitalWrite(5,LOW);   // set the drain pump on
   
    CurrentTaskInterval = minutes_in_ms(10);
    break;


  case 1:
   digitalWrite(6, LOW); // set the Solenoid Valve on
    digitalWrite(2, LOW);  // set the PH down on
    digitalWrite(3, LOW); // set the B nutrient solution on
    digitalWrite(4, LOW); // set the A nutrient solution on
    CurrentTaskInterval = seconds_in_ms(1);
break;

case 2:
   digitalWrite(6, LOW); // set the Solenoid Valve on
   digitalWrite(3, LOW); // set the B nutrient solution on
    digitalWrite(4, LOW); // set the A nutrient solution on
    CurrentTaskInterval = seconds_in_ms(2);

break;


case 3:
digitalWrite(6, LOW); // set the Solenoid Valve on
CurrentTaskInterval = seconds_in_ms(7);

break;

case 4:
digitalWrite(7, HIGH); // set the Nutrient Heater on
// Start over again in one week from the START of the cycle
    Task = 0;
    LastTaskTime = CycleStartTime;
    CurrentTaskInterval = weeks_in_ms(1);
    break;
  }
}


AWOL

Quote
I tweaked my sketch a little and now it wont load,

If you've disabled auto-reset, you'll have to manually reset to upload.
"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.

perkunas

No I haven't disabled the reset yet, I need to buy some parts, my problem is with the program now. IDE doesn't like it,
Ive got errors after tweaking it a bit. where did I go wrong?
tnx

AWOL

It won't load if it doesn't compile.
"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.

perkunas

That's what I mean, I tweaked it, and screwed up something, it wont compile.
here's what I have so far, where did I go wrong?.

Code: [Select]
  //  Some macros for defining time intervals in milliseconds
#define seconds_in_ms(s) ((s)*1000UL)
#define minutes_in_ms(m) ((m)*60UL*1000UL)
#define hours_in_ms(h)   ((h)*60UL*60UL*1000UL)
#define days_in_ms(d)    ((d)*24UL*60UL*60UL*1000UL)
#define weeks_in_ms(w)   ((w)*7UL*24UL*60UL*60UL*1000UL)

unsigned long CycleStartTime = 0;
unsigned long LastTaskTime = 0;
unsigned long CurrentTaskInterval = 0;
unsigned Task = 0;
int incomingbyte;
void setup()
{
  Serial.begin(9600);
   pinMode(2, OUTPUT);     //PH down
   pinMode(3, OUTPUT);  //B nutrient solution
   pinMode(4, OUTPUT);  //A nutrient solution
   pinMode(5, OUTPUT);  //Drain pump
   pinMode(6, OUTPUT);   //Solenoid Valve
   pinMode(7, OUTPUT);  //Nutrient Heater
}
   digitalWrite(2, HIGH);     //PH down off
  digitalWrite(3, HIGH);  //B nutrient solution off
  digitalWrite(4, HIGH);  //A nutrient solution off
  digitalWrite(5, HIGH);  //Drain pump off
  digitalWrite(6, HIGH);   //Solenoid Valve off
  digitalWrite(7, HIGH);  //Nutrient Heater off
{

if (Serial.available() > 0) {
incomingbyte = Serial.read();
}
if(incomingbyte == 112){
digitalWrite(2, LOW);
delay(1000);
digitalWrite(2, HIGH);
}
if(incomingbyte == 110){
digitalWrite(3, LOW);
digitalWrite(4,LOW);
delay(1000);
digitalWrite(3, HIGH);
digitalWrite(4,HIGH);
}

  unsigned long currentTime = millis();

  // If the time has not yet come to perform a task
  if (currentTime - LastTaskTime < CurrentTaskInterval)
    return;   // Nothing to do

  LastTaskTime = currentTime;

  switch (Task++)
  {
  case 0:
    CycleStartTime = currentTime;  // Remember this time

   digitalWrite(5,LOW);   // set the drain pump on
   
    CurrentTaskInterval = minutes_in_ms(10);
    break;


  case 1:
   digitalWrite(6, LOW); // set the Solenoid Valve on
    digitalWrite(2, LOW);  // set the PH down on
    digitalWrite(3, LOW); // set the B nutrient solution on
    digitalWrite(4, LOW); // set the A nutrient solution on
    CurrentTaskInterval = seconds_in_ms(1);
break;

case 2:
   digitalWrite(6, LOW); // set the Solenoid Valve on
   digitalWrite(3, LOW); // set the B nutrient solution on
    digitalWrite(4, LOW); // set the A nutrient solution on
    CurrentTaskInterval = seconds_in_ms(2);

break;


case 3:
digitalWrite(6, LOW); // set the Solenoid Valve on
CurrentTaskInterval = seconds_in_ms(7);

break;

case 4:
digitalWrite(7, HIGH); // set the Nutrient Heater on
// Start over again in one week from the START of the cycle
    Task = 0;
    LastTaskTime = CycleStartTime;
    CurrentTaskInterval = weeks_in_ms(1);
    break;
  }
}

AWOL

Code: [Select]
  pinMode(7, OUTPUT);  //Nutrient Heater
}
  digitalWrite(2, HIGH);     //PH down off

right there.

Quote
That's what I mean, I tweaked it, and screwed up something, it wont compile

compile != load
"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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy