Pages: [1]   Go Down
Author Topic: Keyboard question  (Read 727 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);

}

}
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 594
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you need the ASCII table http://www.asciitable.com/
Logged

Freelance engineer, consultant, contractor. Graduated from UW in 2013.

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh wow
tnx
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  //  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;
  }
}
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  //  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;
  }
}

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It won't load if it doesn't compile.
Logged

"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.

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  //  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;
  }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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
Logged

"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.

Pages: [1]   Go Up
Jump to: