Arm Mbed OS support forum

Joystick input to RGB output

I’m trying to make a program which takes a Joystick input from the application shield and returns a rgb output. Here is the code:

#include “mbed.h”

DigitalIn up(A2);
DigitalIn down(A3);
DigitalIn left(A4);
DigitalIn right(A5);
DigitalIn fire(D4);

class Joystick
{
private:
DigitalIn up, down, left, right, fire;
public:
Joystick(PinName u, PinName d, PinName l, PinName r, PinName f);
bool upPressed(void)
{
if(up==1) {
return true;
} else
return false;

}
bool downPressed(void)
{
    if(down==1) {
        return true;
    } else
        return false;

}
bool leftPressed(void)
{
    if(left==1) {
        return true;
    } else
        return false;

}
bool rightPressed(void)
{
    if(right==1) {
        return true;
    } else
        return false;

}
bool firePressed(void)
{
    if(fire==1) {
        return true;
    } else
        return false;

}

};

class LED
{

protected:
DigitalOut outputSignal;
bool status;

public:
LED(PinName pin) : outputSignal(pin)
{
off();
}

void on(void)
{
    outputSignal = 0;
    status = true;
}

void off(void)
{
    outputSignal = 1;
    status = false;
}



bool getStatus(void)
{
    return status;
}

};

int main()
{
Joystick joystick1(A2, A3, A4, A5, D4);
LED redLED(D5);
LED greenLED(D9);
LED blueLED(D8);
LED yellowLED(D5, D9);
LED whiteLED(D5, D9, D8);

while(1) {
    if(joystick1.upPressed()==true) {
        redLED.on();
    } else if(joystick1.downPressed()==true) {
        greenLED.on();
    } else if(joystick1.leftPressed()==true) {
        blueLED.on();
    }
     else if(joystick1.rightPressed()==true){
         yellowLED.on();
    }
     else if(joystick1.firePressed()==true){
        whiteLED.on();
}

}
}

What’s wrong with my code?

Thanks

Hello Jonathon,

  • You should add definition for the Joystick class constructor to initialize data members. For example:
Joystick::Joystick(PinName u, PinName d, PinName l, PinName r, PinName f) :
    up(u), down(d), left(l), right(r), fire(f) 
{ }
  • The LED class has only one constructor defined which takes single argument. However, you are trying to pass two and three arguments to the LED's constructor:
    LED yellowLED(D5, D9);
    LED whiteLED(D5, D9, D8);

Best regards, Zoltan

Thank you I now understand

Hello,

please, be so kind and use ``` before and after your code, for good code formatting in your post.

Zoltan was faster so I only add :slight_smile:

Also for full RGB potencial you need to use PWMOut instead of DigitalOut, I think and for that is something already done here.

I not tested it but it looks ok

BR, Jan