I have made a transmission controller and the code works fine as it is. But i would like to add debounce function into to the code, since the shifts would somethimes not be recognised when pushing the buttons. I am far from a good coder, so this code is very simple and I understand everything in it. But I cant figure out how to add debounce to the buttons without advancing the code too much for me too not understand it anymore.
Could anyone come with any help or tips please? See attached code.
int gear = 1;
// output pins for transistor controlled solenoids
int solA = 4;
int solB = 5;
int solC = 6;
int solD = 7;
int solE = 8;
int solTCC = 9;
int solLinePress = 10;
// input pins for up down / with pullup resistors
int gearup = 2;
int geardown = 3;
// Pin out for single 7 digit display
int a=22;
int b=23;
int c=24;
int d=25;
int e=26;
int f=27;
int g=28;
void setup() {
pinMode(solA, OUTPUT);
pinMode(solB, OUTPUT);
pinMode(solC, OUTPUT);
pinMode(solD, OUTPUT);
pinMode(solE, OUTPUT);
pinMode(solTCC, OUTPUT);
pinMode(solLinePress, OUTPUT);
pinMode(gearup, INPUT);
pinMode(geardown, INPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
}
void loop() {
if(gear == -1){ // Reverse
digitalWrite(solA, 1);
digitalWrite(solB, 1);
digitalWrite(solC, 1);
digitalWrite(solD, 0);
digitalWrite(solE, 1);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
if(gear == 0){ // Neutral
digitalWrite(solA, 1);
digitalWrite(solB, 1);
digitalWrite(solC, 1);
digitalWrite(solD, 1);
digitalWrite(solE, 1);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
if(gear == 1){ // 1th gear
digitalWrite(solA, 1);
digitalWrite(solB, 1);
digitalWrite(solC, 0);
digitalWrite(solD, 1);
digitalWrite(solE, 1);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
if(gear == 2){ // 2nd Gear
digitalWrite(solA, 1);
digitalWrite(solB, 1);
digitalWrite(solC, 1);
digitalWrite(solD, 1);
digitalWrite(solE, 0);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
if(gear == 3){ // 3rd Gear
digitalWrite(solA, 0);
digitalWrite(solB, 1);
digitalWrite(solC, 0);
digitalWrite(solD, 1);
digitalWrite(solE, 0);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
if(gear == 4){ // 4th Gear
digitalWrite(solA, 0);
digitalWrite(solB, 1);
digitalWrite(solC, 1);
digitalWrite(solD, 0);
digitalWrite(solE, 0);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
if(gear == 5){ // 5th Gear
digitalWrite(solA, 0);
digitalWrite(solB, 1);
digitalWrite(solC, 0);
digitalWrite(solD, 0);
digitalWrite(solE, 1);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
if(gear == 6){ / // 6th Gear
digitalWrite(solA, 0);
digitalWrite(solB, 0);
digitalWrite(solC, 0);
digitalWrite(solD, 0);
digitalWrite(solE, 0);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
if(gear == 7){ // 7th Gear
digitalWrite(solA, 1);
digitalWrite(solB, 0);
digitalWrite(solC, 0);
digitalWrite(solD, 0);
digitalWrite(solE, 1);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
if(gear == 8){ // 8th Gear
digitalWrite(solA, 1);
digitalWrite(solB, 0);
digitalWrite(solC, 1);
digitalWrite(solD, 0);
digitalWrite(solE, 0);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
//takes care of gear switching
delay(150); //delay to prevent going through gears too quick from holding the button or pressing too long
gear += digitalRead(geardown) - digitalRead(gearup); // non debounced! But may not be a problem because of the delay by gear change
if(gear < -1) gear = -1;
if(gear > 8) gear = 8;
//limits to actual gearset
}
Because the question asks specifically about how to debounce i will link you here to the official arduino example that is also contained in the IDE, libraries exist online with a brief search i found this one and you could also hardware debounce the inputs using resistors and capacitors.
Related
Hi I'm very much a beginner in the world of Arduino and I just got my Arduino Uno, I wanted to try something out, so I found some instructions on a traffic light. Pretty simple, but I wanted to ad a pedestrian crossing button. However, when I did that, without touching the button, after three cycles of green yellow red, it would act if I pushed the button. I have decided (from removing components) that the code is the problem. Does anybody know how to fix this?
int red = 10;
int yellow = 9;
int green = 8;
int button = 12;
int pedgreen = 11;
int pedred = 13;
int pedcross;
int count;
void setup(){
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(pedgreen, OUTPUT);
pinMode(pedred, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
pedcross = 0;
digitalWrite(pedred, HIGH);
pedcross = 0;
changeLights();
pedcross = 0;
}
void changeLights(){
pedc = 0;
digitalWrite(pedred, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
count = 40;
while (count > 0){
if (digitalRead(button) == HIGH) {
delay(15);
if (digitalRead(button) == HIGH) {
pedcross = 1;
}
}
count = count - 1;
delay(100);
}
digitalWrite(yellow, HIGH);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
count = 20;
while (count > 0){
if (digitalRead(button) == HIGH) {
delay(15);
if (digitalRead(button) == HIGH) {
pedcross = 1;
}
}
count = count - 1;
delay(100);
}
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
if (pedcross == 1){
pedcross = 0;
pedcycle();
pedcross = 0;
}
else {
delay(5000);
}
}
void pedcycle(){
pedcross = 0;
digitalWrite(pedgreen, HIGH);
digitalWrite(pedred, LOW);
delay(3000);
digitalWrite(pedgreen, LOW);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(500);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(500);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(500);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(1500);
pedcross = 0;
}
You have to pull your input either up or down using a resistor, or you can use the built-in pull-up resistor of the Arduino by initializing the input pin like this:
pinMode(button, INPUT_PULLUP);
So I have a project to make for school in which we had to make an arduino project from scratch. I'm relatively new to arduino and his coding and made a simple, yet fun project. I had the idea of making a sort of safe where you have to enter a key and after that, use a potentiometer to make the right combination to open the safe. I don't know what I did wrong but when I uploaded it to my arduino, the program got to the first lines and stopped after printing the first line to the serial monitor. There the program is stuck and doesn't do anything. The weird thing is that it worked 5 minutes ago but now it just wont. Can anyone help me please? My deadline is in a few hours and I can't fix it... Here is the code and a schematic:
Picture of the setup. The 2 resistors on the left are 1k, the other 2 are 10k.
//variabelen
const int pot = A0;
const int pinA = 6;
const int pinB = 5;
const int pinC = 2;
const int pinD = 3;
const int pinE = 4;
const int pinF = 7;
const int pinG = 8;
const int pinP = 9;
const int enter = 11;
const int reset = 12;
byte enterState = 0;
byte lastEnterState = 0;
byte resetState = 0;
byte lastResetState = 0;
int enterTimer = 0;
String wachtwoord;
String ingegeven = "";
//setup
void setup(){
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinP, OUTPUT);
pinMode(enter, INPUT);
pinMode(reset, INPUT);
Serial.begin(9600);
Serial.println("Geef een wachtwoord op voor de safe.");
wachtwoord = Serial.read();
}
//eigen methoden
void dispChar(int getal){
switch(getal){
case 0:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
break;
case 1:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case 2:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
break;
case 3:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
break;
case 4:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case 5:
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case 6:
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case 7:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case 8:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case 9:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
}
}
//Kijk of enter knop is ingedrukt
void onEnter(){
enterState = digitalRead(enter);
if(enterState == HIGH){
enterTimer++;
if(enterState != lastEnterState){
ingegeven += map(analogRead(pot), 0, 1023, 0, 9);
Serial.println(ingegeven);
enterTimer = 0;
}
}
delay(50); //Voorkomt bounce.
if(enterTimer > 50){
Serial.println("\nRESET");
enterTimer = 0;
ingegeven = "";
}
lastEnterState = enterState;
check();
}
bool check(){
if(ingegeven.equals(wachtwoord)){
return true;
} else{
if(ingegeven.length() > wachtwoord.length()){
Serial.println("FOUT!");
ingegeven = "";
return false;
}
}
}
//Kijk of reset knop is ingedrukt
void onReset(){
resetState = digitalRead(reset);
if(resetState != lastResetState){
if(resetState == HIGH){
ingegeven = "";
Serial.println("Geef een wachtwoord op voor de safe.");
wachtwoord = Serial.read();
}
}
delay(50); //Voorkomt bounce.
lastResetState = resetState;
}
//loop
void loop(){
while(!check()){
int getal = map(analogRead(pot), 0, 1023, 0, 9);
digitalWrite(pinP, HIGH);
dispChar(getal);
onEnter();
onReset();
}
Serial.println("CODE CORRECT!")
}
Thanks for the help!!
I need to create an Arduino Wave Generator, but I realy can't get it to work properly.
The generator must output Sine, Square, Triangle and Sawtooth waves, I got the square one working, I tried a lot of things, but can't get the others to work properly.
Here's my circuit:
And here's the code that I'm using
int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int display = 13;
int inBtn = 8;
int btnSt = 0;
int btnLastSt = 0;
int cont = 0;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(display, OUTPUT);
pinMode(inBtn, INPUT);
}
void loop() {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
switch(cont){
case 0:
digitalWrite(led1, HIGH);
digitalWrite(display, HIGH);
delay(250);
digitalWrite(display, LOW);
delay(250);
break;
case 1:
digitalWrite(led2, HIGH);
break;
case 2:
digitalWrite(led3, HIGH);
break;
case 3:
digitalWrite(led4, HIGH);
break;
}
if (digitalRead(inBtn) == LOW) {
cont++;
delay(150);
}
if (cont == 4) cont = 0;
if (btnLastSt != cont){
btnLastSt = cont;
}
}
The others waves would go inside the other 'cases'.
I'm sorry about the lack of info and research, but at this point I'm quite desperate (it's an assignment).
If someone could help me a bit on this, would be great.
UPDATE
Here's my final circuit and code, after Gerhard's help.
int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int display = 11;
int inBtn = 8;
int btnSt = 0;
int btnLastSt = 0;
int cont = 0;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(display, OUTPUT);
pinMode(inBtn, INPUT);
}
void loop() {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
switch(cont){
case 0:
//Square
digitalWrite(led1, HIGH);
digitalWrite(display, HIGH);
delay(200);
digitalWrite(display, LOW);
delay(200);
break;
case 1:
//Sawtooth
digitalWrite(led2, HIGH);
for(int i = 0; i < 255; i++){
analogWrite(display, i);
delay(1);
}
analogWrite(display, 0);
delay(1);
break;
case 2:
//Triangular
digitalWrite(led3, HIGH);
for (int i = 0; i < 254; i++){
analogWrite(display, i);
delay(1);
}
for (int i2 = 254; i2 > 0; i2--){
analogWrite(display, i2);
delay(1);
}
break;
case 3:
//Sine
digitalWrite(led4, HIGH);
analogWrite(display, 10); delay(10);
analogWrite(display, 19); delay(10);
analogWrite(display, 45); delay(10);
analogWrite(display, 84); delay(10);
analogWrite(display, 130); delay(10);
analogWrite(display, 176); delay(10);
analogWrite(display, 215); delay(10);
analogWrite(display, 241); delay(10);
analogWrite(display, 250); delay(10);
analogWrite(display, 241); delay(10);
analogWrite(display, 215); delay(10);
analogWrite(display, 176); delay(10);
analogWrite(display, 130); delay(10);
analogWrite(display, 84); delay(10);
analogWrite(display, 45); delay(10);
analogWrite(display, 19); delay(10);
break;
}
if (digitalRead(inBtn) == LOW) {
cont++;
delay(100);
}
if (cont == 4) cont = 0;
if (btnLastSt != cont){
btnLastSt = cont;
}
}
It's not perfect, but it's good enough haha.
Your hardware setup is insufficient for what you want to achieve.
You need an analog output but you are using a digital output. You have to add a DAC (Digital to Analog Converter) (MAX98357A example) or use the PWM (Pulse Width Modulation) outputs (pins 3,5,6,9-11). Both of these would also require an additional LPF (low pass filter) on the output.
For PWM you setup the output pinMode(pin, OUTPUT) and can then use analogWrite(pin, value) with values on a scale of 0 - 255.
I have a small 7 segment led display and I wondered if I could get it to show me the last digit of the minute. Downloaded the Time library for Arduino and I tried several methods, with switch case and if else if, but all give the same result, the LED display shows only 0... Could you pinpoint what I did wrong?
#include <Time.h>
#include <TimeLib.h>
int led1=2;
int led2=3;
int led3=4;
int led4=5;
int led5=6;
int led6=7;
int led7=8;
int led8=9;
void setup() {
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
pinMode(led6,OUTPUT);
pinMode(led7,OUTPUT);
pinMode(led8,OUTPUT);
}
void loop(){
time_t t=minute();
int digit1=t%10;
if(digit1==1){
digitalWrite(led5, HIGH);
digitalWrite(led3, HIGH);
}else if(digit1==2)
{
digitalWrite(led6, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
} else if(digit1==3)
{
digitalWrite(led4, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led2, HIGH);
}else if(digit1==4)
{
digitalWrite(led4, HIGH);
digitalWrite(led7, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led3, HIGH);
}else if(digit1==5)
{
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led2, HIGH);
}else if(digit1==6)
{
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
}else if(digit1==7)
{
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led3, HIGH);
}else if(digit1==8)
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
}else if(digit1==9)
{
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
}else if(digit1==0)
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
}
}
You never write any of the pins to go low. When the next minute hits, the proper LEDS are probably turning on, but the old ones don't go off, so you don't see any change.
To avoid turning all the LEDS off and on again every cycle, I would store the last known digit and at each loop iteration, see if the digit has changed. If it has, turn off all of the lights and then run the code you posted here.
You haven't added low conditions hence when the pin is going high it is remaining high. Also, I have checked and I think that your wiring is not correct. I'm posting complete code below and connections accordingly.
#include <Time.h>
#include <TimeLib.h>
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int dp = 9;
void setup() {
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
pinMode(led6,OUTPUT);
pinMode(led7,OUTPUT);
pinMode(led8,OUTPUT);
}
void loop(){
time_t t=minute();
int digit1=t%10;
if(digit1==0) {
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
digitalWrite(dp, LOW);
}
else if(digit1==1) {
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(dp, LOW);
}
else if(digit1==2) {
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
digitalWrite(dp, LOW);
}
else if(digit1==3) {
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
digitalWrite(dp, LOW);
}
else if(digit1==4) {
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(dp, LOW);
}
else if(digit1==5) {
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(dp, LOW);
}
else if(digit1==6) {
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(dp, LOW);
}
else if(digit1==7) {
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(dp, LOW);
}
else if(digit1==8) {
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(dp, LOW);
}
else if(digit1==9) {
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(dp, LOW);
}
}
So i need help changing the if else statements into switch case statements as i'm new to coding in arduino and can't seem to get it to work. The program is supposed to blink the LED, sound the speaker, light up the bulb, spin the fan and also display the selected message onto the 16x2 lCD when the conditions in the if else statements are fufilled.
#include <LiquidCrystal.h>
//Pins of LCD RS E D4 D5 D6 D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Pins of Arduino
int tempPin = A0; //LM35 Pin
int ldrPin = A1; //LDR Pin
int led = 6; //LED Pin
int tempMax = 100;
int buzzer = 13;
float temp;
float tempC;
int lightReading;
int numTones = 2;
int tones[] = {440, 349};
int TIP120pin = 5;
int bulb = A2;
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(tempPin, INPUT);
pinMode(ldrPin, INPUT);
pinMode(buzzer,OUTPUT);
pinMode(TIP120pin, OUTPUT);
pinMode(bulb, OUTPUT);
lcd.clear();
beep(100);
}
void loop()
{
tempC = (5.0 * analogRead(tempPin) * 100.0) / 1024;
temp = tempC;
lightReading = analogRead(ldrPin);
lcd.print("TEMP:");
lcd.setCursor(5, 0);
lcd.print(temp); // display the temperature
lcd.print("C");
// Display Light on second row
lcd.setCursor(0, 1);
lcd.print("Light ");
lcd.setCursor(6, 1);
lcd.print(lightReading);
delay(500);
analogWrite(TIP120pin, 0);
analogWrite(bulb, 0);
if (temp >=35) // if temp is higher than max temp
{
lcd.clear();
analogWrite(TIP120pin, 255);
lcd.setCursor(3, 0);
lcd.print("Room Hot!!");
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
}
else if (temp >=30 && temp<35)
{
lcd.clear();
analogWrite(TIP120pin, 255);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Warm!");
delay(200);
}
else if (lightReading >100 && lightReading <= 200)
{
lcd.clear();
analogWrite(bulb, 150);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
// Display Light on second row
lcd.setCursor(3, 0);
lcd.print("Room Dim");
delay(200);
analogWrite(TIP120pin, 0);
}
else if (lightReading <=100)
{
lcd.clear();
analogWrite(bulb, 255);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Dark");
delay(200);
analogWrite(TIP120pin, 0);
}
else if(temp>=35 && lightReading>100 && lightReading<=200)
{
lcd.clear();
analogWrite(bulb, 150);
analogWrite(TIP120pin, 255);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Hot!");
lcd.setCursor(3, 1);
lcd.print("Room Dim!");
delay(200);
}
else if(temp>=35 && lightReading<=100)
{
lcd.clear();
analogWrite(bulb, 255);
analogWrite(TIP120pin, 255);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Hot!");
lcd.setCursor(3, 1);
lcd.print("Room Dark!");
delay(200);
}
else if(temp>=30 && temp<35 && lightReading>=100 && lightReading <=200)
{
lcd.clear();
analogWrite(bulb, 150);
analogWrite(TIP120pin, 150);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Warm!");
lcd.setCursor(3, 1);
lcd.print("Room Dim!");
delay(200);
}
else if (temp>=30 && temp<35 && lightReading<100)
{
lcd.clear();
analogWrite(bulb, 255);
analogWrite(TIP120pin, 255);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Hot!!");
lcd.setCursor(3, 1);
lcd.print("Room Dark!");
delay(200);
}
else
{
digitalWrite(led, LOW);
digitalWrite(buzzer, LOW);
analogWrite(bulb, LOW);
lcd.print("TEMP:");
lcd.setCursor(5, 0);
lcd.print(temp);
lcd.print("C");
// Display Light on second row
lcd.setCursor(0, 1);
lcd.print("Light ");
lcd.setCursor(6, 1);
lcd.print(lightReading);
}
delay(500);
lcd.clear();
Serial.print("Light, "); Serial.print(lightReading); Serial.print('\n');
}
void beep(unsigned char delayms){
for(int x=0; x<numTones; x++)
{
tone(buzzer, tones[x]);
delay(delayms);
}
noTone(buzzer);
}
if you want to convert if statement to Switch ,i.e i am assuming that you want to improve the readability of the code ,you can use if statement in loop and put switch in a function ..check the code below
void loop()
{
tempC = (5.0 * analogRead(tempPin) * 100.0) / 1024;
temp = tempC;
lightReading = analogRead(ldrPin);
lcd.print("TEMP:");
lcd.setCursor(5, 0);
lcd.print(temp); // display the temperature
lcd.print("C");
// Display Light on second row
lcd.setCursor(0, 1);
lcd.print("Light ");
lcd.setCursor(6, 1);
lcd.print(lightReading);
delay(500);
analogWrite(TIP120pin, 0);
analogWrite(bulb, 0);
if (temp >=35) // if temp is higher than max temp
{
doSomething('a');
}
else if (temp >=30 && temp<35)
{
doSomething('b');
}
else if (lightReading >100 && lightReading <= 200)
{
doSomething('c');
}
else if (lightReading <=100)
{
doSomething('d');
}
else if(temp>=35 && lightReading>100 && lightReading<=200)
{
doSomething('e');
}
else if(temp>=35 && lightReading<=100)
{
doSomething('f');
}
else if(temp>=30 && temp<35 && lightReading>=100 && lightReading <=200)
{
doSomething('g');
}
else if (temp>=30 && temp<35 && lightReading<100)
{
doSomething('h');
}
else
{
doSomething('i');
}
and put your different condition in the switch statement in the doSomething() function like this
void doSomething(condition){
switch(condition){
case 'a' : // if temp is higher than max temp
lcd.clear();
analogWrite(TIP120pin, 255);
lcd.setCursor(3, 0);
lcd.print("Room Hot!!");
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
break;
case 'b' :
lcd.clear();
analogWrite(TIP120pin, 255);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Warm!");
delay(200);
break;
case 'c' :
lcd.clear();
analogWrite(bulb, 150);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
// Display Light on second row
lcd.setCursor(3, 0);
lcd.print("Room Dim");
delay(200);
analogWrite(TIP120pin, 0);
break;
case 'd':
lcd.clear();
analogWrite(bulb, 255);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Dark");
delay(200);
analogWrite(TIP120pin, 0);
break;
case 'e':
lcd.clear();
analogWrite(bulb, 150);
analogWrite(TIP120pin, 255);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Hot!");
lcd.setCursor(3, 1);
lcd.print("Room Dim!");
delay(200);
break;
case 'f':
lcd.clear();
analogWrite(bulb, 255);
analogWrite(TIP120pin, 255);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Hot!");
lcd.setCursor(3, 1);
lcd.print("Room Dark!");
delay(200);
break;
case 'g':
lcd.clear();
analogWrite(bulb, 150);
analogWrite(TIP120pin, 150);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Warm!");
lcd.setCursor(3, 1);
lcd.print("Room Dim!");
delay(200);
break;
case 'h':
lcd.clear();
analogWrite(bulb, 255);
analogWrite(TIP120pin, 255);
digitalWrite(led, HIGH);
delay(200);
beep(50);
digitalWrite(led, LOW);
delay(200);
lcd.setCursor(3, 0);
lcd.print("Room Hot!!");
lcd.setCursor(3, 1);
lcd.print("Room Dark!");
delay(200);
break;
default:
digitalWrite(led, LOW);
digitalWrite(buzzer, LOW);
analogWrite(bulb, LOW);
lcd.print("TEMP:");
lcd.setCursor(5, 0);
lcd.print(temp);
lcd.print("C");
// Display Light on second row
lcd.setCursor(0, 1);
lcd.print("Light ");
lcd.setCursor(6, 1);
lcd.print(lightReading);
break;
}
}
In Java, the switch statement takes this form:
switch (expression)
{
case constant:
statements;
break;
[ case constant-2:
statements;
break; ] ...
[ default:
statements;
break; ] ...
}
The switch statement works with the byte, short, char, and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.
Your nested ifs with their boolean expressions are not a good fit for switch.