project 2015

hi everyone, i’m thomas, ( i’m italian, so, sorry for my bad english ahah), for the end of the school i’m trying to make a little project: use arduino as a keyboard, giving “inputs” with some interrupts, and programming the processor ( ATMEGA16u2, i have Arduino UNO Rev 3) as a USB-HID. so, for each input i give to arduino, i can write in the computer a character as a normal keyboard ( is a project for gaming, so i have only 6 inputs).

now, i have a problem programming the Arduino ( not the processor)

this is my program:

#include <SoftwareSerial.h>
void setup();
uint8_t buf[8] = { 0 }; /* Keyboard report buffer */

#define KEY_LEFT_CTRL 0x01
#define KEY_LEFT_SHIFT 0x02
#define KEY_RIGHT_CTRL 0x10
#define KEY_RIGHT_SHIFT 0x20

void setup()  
{
  Serial.begin(9600); //connection between chips
  delay(200);


pinMode(0,INPUT);

pinMode(1,INPUT);

pinMode(2,INPUT);

pinMode(3,INPUT);

pinMode(4,INPUT);

pinMode(5,INPUT);

pinMode(8,OUTPUT);

}



void loop() 

{ 

  type(PORTD);

}

void type(char *str) 

{  
    
    char *chp = str;
    delay(100);
    while (*chp) {
     
 if ((*chp >= 'a') && (*chp <= 'z')) {
     buf[2] = *chp - 'a' + 4; //Converts from char to usb code from HUT1_11.pdf 
 } else if ((*chp >= 'A') && (*chp <= 'Z')) {
     buf[0] = KEY_LEFT_SHIFT; /* Caps */
     buf[2] = *chp - 'A' + 4; //Converts from char to usb code from HUT1_11.pdf 
 } else if ((*chp >= '0') && (*chp <= '9')) {
     buf[2] = *chp - '0' + 30; //Converts from char to usb code from HUT1_11.pdf 
 } else {
     switch (*chp) {
     case '1':
      buf[2] = 0x04; // a
  break;
     case '2':
      buf[2] = 0x26; // w
  break;
     case '4':
      buf[2] = 0x22; // s
  break;
     case '8':
      buf[2] = 0x07; // d
  break;
     case '16':
      buf[2] = 0x44; // space
  break;
     case '32':
      buf[2] = 0x04; // q
  break;
     default:
         /* Character not handled. To do: add rest of chars from HUT1_11.pdf */
  //buf[2] = 0x37; // Period
  break;
     }
     
      Serial.write(buf, 7); // Send keypress
 buf[0] = 0;
 buf[2] = 0;
 Serial.write(buf, 7); // Release key
 chp++;
    

}}}

and this is my error report:

Arduino: 1.6.4 (Windows 7), Плата"Arduino Uno"

?????????? ?????????? SoftwareSerial ? ?????: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial 



C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10604 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial C:\Users\KRIKKI~1\AppData\Local\Temp\build1776169019545733888.tmp\programma_arduino_keyboard.cpp -o C:\Users\KRIKKI~1\AppData\Local\Temp\build1776169019545733888.tmp\programma_arduino_keyboard.cpp.o 

programma_arduino_keyboard.ino: In function 'void loop()':
programma_arduino_keyboard.ino:38:13: error: invalid conversion from 'uint8_t {aka unsigned char}' to 'char*' [-fpermissive]
programma_arduino_keyboard.ino:4:6: error:   initializing argument 1 of 'void type(char*)' [-fpermissive]
invalid conversion from 'uint8_t {aka unsigned char}' to 'char*' [-fpermissive]

i don’t know why the conversion is invalid, and how can i fix this problem.

can someone be so graceful to help me please ? :slight_smile:

thanks,

thomas

if ((*chp >= ‘a’) && (*chp <= ‘z’)) {
buf[2] = *chp - ‘a’ + 4; //Converts from char to usb code from HUT1_11.pdf
} else if ((*chp >= ‘A’) && (chp <= ‘Z’)) {
buf[0] = KEY_LEFT_SHIFT; /
Caps */
buf[2] = *chp - ‘A’ + 4; //Converts from char to usb code from HUT1_11.pdf
} else if ((*chp >= ‘0’) && (*chp <= ‘9’)) {
buf[2] = *chp - ‘0’ + 30; //Converts from char to usb code from HUT1_11.pdf
} else {

why you are using characters ?! it’s better use symbols, you will have some errors in this way
anyway look at my blog, you can found a serial communication softwares if you need the whole code to edit it just tell me, you can use them :slight_smile:

Would it be better to do something like this

pVal = PORTD;
type(pVal);

But I am not good at type conversion in C

I don’t understand, either, why you use a variable called str to receive a byte value

…R