make public

Dependencies:   mbed

Committer:
BertieHarte
Date:
Fri Mar 26 10:20:02 2021 +0000
Revision:
0:325feea5981d
make public

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BertieHarte 0:325feea5981d 1 //Write a program to turn on LED if analog value from any of the potentimeter is greater than 50% and turn it off when the value is less than or equal to 50%.
BertieHarte 0:325feea5981d 2 //copy and paste the final code you have in the answer section.
BertieHarte 0:325feea5981d 3
BertieHarte 0:325feea5981d 4 #include "mbed.h"
BertieHarte 0:325feea5981d 5
BertieHarte 0:325feea5981d 6 DigitalOut R(p23); //Red pin of RGB LED
BertieHarte 0:325feea5981d 7 DigitalOut G(p24); //Green pin of RGB LED
BertieHarte 0:325feea5981d 8 DigitalOut B(p25); //Blue pin of RGB LED
BertieHarte 0:325feea5981d 9 AnalogIn pot1(p19);// input from pot 1
BertieHarte 0:325feea5981d 10 AnalogIn pot2(p20);// input from pot 2
BertieHarte 0:325feea5981d 11 void on(){ // function to turn on white LED
BertieHarte 0:325feea5981d 12 R = G = B = 0;
BertieHarte 0:325feea5981d 13 }
BertieHarte 0:325feea5981d 14
BertieHarte 0:325feea5981d 15 void off(){ // function to turn off white led
BertieHarte 0:325feea5981d 16 R = G = B = 1;
BertieHarte 0:325feea5981d 17 }
BertieHarte 0:325feea5981d 18
BertieHarte 0:325feea5981d 19 int main() {
BertieHarte 0:325feea5981d 20 R = G = B = 1; // init RGB led to off (all 1)
BertieHarte 0:325feea5981d 21 while(1) {
BertieHarte 0:325feea5981d 22 float p1 = (pot1*100); // converts the pot reading to % (multiple by 100)
BertieHarte 0:325feea5981d 23 float p2 = (pot2*100); // converts the pot reading to % (multiple by 100)
BertieHarte 0:325feea5981d 24 // printf("pot1 value = %f \n \r", p1);
BertieHarte 0:325feea5981d 25 // printf("pot2 value = %f \n \r", p2);
BertieHarte 0:325feea5981d 26 if(p1 >50 || p2 >50){ // if value of p1 OR p2 > 50 (technically 50.00...) the LEd will turn on.
BertieHarte 0:325feea5981d 27 on(); // call function "on"
BertieHarte 0:325feea5981d 28 }
BertieHarte 0:325feea5981d 29 else // if the float value of either p1 or p2 is less than or equal to 50 (technically 50.00...) the LEd will go off.
BertieHarte 0:325feea5981d 30 {
BertieHarte 0:325feea5981d 31 off(); // call function "off"
BertieHarte 0:325feea5981d 32 }
BertieHarte 0:325feea5981d 33 }
BertieHarte 0:325feea5981d 34 }
BertieHarte 0:325feea5981d 35