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.
Related
im getting a error like
error: 'class WiFiServer' has no member named 'arg'
my code
#define ENA 14 // Enable/speed motors Right GPIO14(D5)
#define ENB 12 // Enable/speed motors Left GPIO12(D6)
#define IN_1 13 // L298N in1 motors Rightx GPIO13(D7)
#define IN_2 15 // L298N in2 motors Right GPIO15(D8)
#define IN_3 2 // L298N in3 motors Left GPIO2(D4)
#define IN_4 0 // L298N in4 motors Left GPIO0(D3)
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
String command; //String to store app command state.
int speedCar = 100; // 400 - 1023.
const char* ssid = "your wifi";
const char* password = "your password";
WiFiServer server(80);
IPAddress local_IP(192, 168, 1, 184);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
void setup() {
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);
Serial.begin(115200);
// Connecting WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected");
server.begin();
Serial.println("Server Connected");
Serial.print("Go to this link: https://");
Serial.println(WiFi.localIP());
}
void goAhead(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goBack(){
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void goRight(){
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goLeft(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void goAheadRight(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goAheadLeft(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goBackRight(){
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void goBackLeft(){
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void stopRobot(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("Waiting for new client");
while (!client.available()) {
delay(1);
}
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
if (request.indexOf("/State=F")) command = "F";
else if (request.indexOf("/State=B")) command = "B";
else if (request.indexOf("/State=L")) command = "L";
else if (request.indexOf("/State=R")) command = "R";
else if (request.indexOf("/State=I")) command = "I";
else if (request.indexOf("/State=G")) command = "G";
else if (request.indexOf("/State=J")) command = "J";
else if (request.indexOf("/State=H")) command = "H";
else if (request.indexOf("/State=0")) command = "0";
else if (request.indexOf("/State=1")) command = "1";
else if (request.indexOf("/State=2")) command = "2";
else if (request.indexOf("/State=3")) command = "3";
else if (request.indexOf("/State=4")) command = "4";
else if (request.indexOf("/State=5")) command = "5";
else if (request.indexOf("/State=6")) command = "6";
else if (request.indexOf("/State=7")) command = "7";
else if (request.indexOf("/State=8")) command = "8";
else if (request.indexOf("/State=9")) command = "9";
else if (request.indexOf("/State=S")) command = "S";
else Serial.println("Wrong command");
command = server.arg("State");
if (command == "F") goAhead();
else if (command == "B") goBack();
else if (command == "L") goLeft();
else if (command == "R") goRight();
else if (command == "I") goAheadRight();
else if (command == "G") goAheadLeft();
else if (command == "J") goBackRight();
else if (command == "H") goBackLeft();
else if (command == "0") speedCar = 100;
else if (command == "1") speedCar = 117;
else if (command == "2") speedCar = 135;
else if (command == "3") speedCar = 153;
else if (command == "4") speedCar = 170;
else if (command == "5") speedCar = 190;
else if (command == "6") speedCar = 205;
else if (command == "7") speedCar = 223;
else if (command == "8") speedCar = 240;
else if (command == "9") speedCar = 255;
else if (command == "S") stopRobot();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<br><button>ahead left</button>");
client.println("<button>ahead</button>");
client.println("<button>ahead right</button><br>");
client.println("<br><button>left</button>");
client.println("<button>stop</button>");
client.println("<button>right</button><br>");
client.println("<br><button>back left</button>");
client.println("<button>back</button>");
client.println("<button>back right</button><br>");
client.println("<button>Speed 0</button>");
client.println("<button>Speed 1</button>");
client.println("<button>Speed 2</button>");
client.println("<button>Speed 3</button>");
client.println("<button>Speed 4</button>");
client.println("<button>Speed 5</button>");
client.println("<button>Speed 6</button>");
client.println("<button>Speed 7</button>");
client.println("<button>Speed 8</button>");
client.println("<button>Speed 9</button>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
}
actually im making a web controlled car where it can be controlled with any device.
thanks in advance.
You've confused WiFiServer for ESP8266WebServer.
WiFiServer simply listens for raw TCP connections and acts as a server for them. It is not and never has been a web server. TCP connections don't have arguments passed to them, so there is no arg method.
You need to rewrite your code to use ESP8266WebServer - you included the header file for it but never used it.
There are plenty of examples on how to use it in its repository.
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);
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.
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 build a bluetooth controled car that i have some problem with the code to it. Im using a switch case Statements to control it from my phone. The code works fine but i got a problem with the default: i want it to stop the car if it doesn't receive anything via the bluetooth. My code doesent seem to execute the default: at all. and i dont know whats the problem.
well here is my code.
int IN1 = 7;
int IN2 = 5;
int IN3 = 4;
int IN4 = 2;
int inByte = 0;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop() {
//read my bluetooth
if (Serial.available() > 0) {
inByte = Serial.read();
Serial.print("I received: ");
Serial.println(inByte);
switch (inByte) {
case 'i':
Serial.println("forward");
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 'j':
Serial.println("left");
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 'l':
Serial.println("right");
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 'k':
Serial.println("reverse");
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
default:
Serial.println("stop");
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
}
}
}