I’m Alexander, I’m an Arduino UNO and C++ beginner.
I’m stuck trying to add a secondary integer for counting values. I suspect that the issue is within the first if statement in the loop.
I have two integers, one for Home and one for Away. Would anyone happen to pinpoint how I can add my secondary integer (Away) within or at least as a member of this if statement?
if (currentHomeState != previousHomeState) {
previousHomeState = currentHomeState;
if (currentHomeState == On) {
if ( (msec - msecLst) > Interval) {
msecLst = msec;
numberOfGoals++;
}
The complete program:
/*
Program for the 1960s Panco Mini-Match table top soccer game.
*/
#include <SSD1320_OLED.h>
// Initialize the display with the follow pin connections.
SSD1320 flexibleOLED(10, 9); //10 = CS, 9 = RES
// Define constants for home and away team.
const int Home = A1;
const int Away = A2;
// Define constants for LED pins.
const int LED_1 = 7; // Pin 7 connected to a LED, turns HIGH when away team score.
const int LED_2 = 8; // Pin 8 connected to a LED, turns HIGH when home team score.
enum { Off = HIGH, On = LOW };
#define Interval 1000
// Initalize and define states for the goal sensors.
int currentHomeState = 0, previousHomeState = 0;
int currentAwayState = 0, previousAwayState = 0;
int numberOfGoals = 0;
unsigned long msecLst = 0;
void setup() {
//Run once
//Initilize the display
flexibleOLED.begin(160, 32); //Display is 160 wide, 32 high
flexibleOLED.clearDisplay(); //Clear display and buffer
//Display Home score
flexibleOLED.setContrast(255);
flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
flexibleOLED.setCursor(45, 7);
flexibleOLED.print(numberOfGoals);
//Display Away score
flexibleOLED.setContrast(255);
flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
flexibleOLED.setCursor(95, 7);
flexibleOLED.print(numberOfGoals);
flexibleOLED.display();
pinMode(Home, INPUT_PULLUP);
pinMode(LED_1, OUTPUT);
Serial.begin(9600);
}
void loop() {
unsigned long msec = millis ();
//Run repeatedly
currentHomeState = digitalRead(Home);
currentAwayState = digitalRead(Away);
if (currentHomeState != previousHomeState) {
previousHomeState = currentHomeState;
if (currentHomeState == On) {
if ( (msec - msecLst) > Interval) {
msecLst = msec;
numberOfGoals++;
Serial.println (numberOfGoals);
flexibleOLED.setContrast(255);
flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
flexibleOLED.setCursor(45, 6);
flexibleOLED.print(numberOfGoals);
flexibleOLED.display();
digitalWrite(LED_1, HIGH);
delay(250);
digitalWrite(LED_1, LOW);
}
if (numberOfGoals == 5) {
digitalWrite(LED_1, HIGH);
delay(250);
digitalWrite(LED_1, LOW);
delay(250);
digitalWrite(LED_1, HIGH);
delay(250);
digitalWrite(LED_1, LOW);
delay(250);
digitalWrite(LED_1, HIGH);
delay(250);
digitalWrite(LED_1, LOW);
delay(250);
digitalWrite(LED_1, HIGH);
delay(250);
digitalWrite(LED_1, LOW);
delay(250);
digitalWrite(LED_1, HIGH);
}
if (numberOfGoals == 5) {
numberOfGoals = 0;
delay(250);
digitalWrite(LED_1, LOW);
}
if (numberOfGoals == 0) {
flexibleOLED.clearDisplay(); // Clear display and buffer
flexibleOLED.setContrast(255);
flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
flexibleOLED.setCursor(45, 6);
flexibleOLED.print(numberOfGoals);
flexibleOLED.display();
Serial.println (0);
}
}
}
}
In another forum a user wrote to me that I needed to make a sub-function with arrays, although this is fairly new to me I came up with something like
int main(int argc, char** argv) {
string teams[2] = {"Home", "Away"};
int numbers[1];
for(int i = 0;i<1;i++)
{
// not yet sure what to add here.
}
for(int i = 1;i<1;i++)
{
// not yet sure what to add here.
}
return 0;
}
Would anyone happen to share their thoughts on whether or not I am close to figuring this one out?
Your time and help are very much appreciated.
In principle you could make your handling of teams scalable by using introducing a team class and instantiating two objects. But since I dont think that there will be more than 2 teams a straight forward solution is to just do the same things for the away team, that you already did for the home team:
#include <SSD1320_OLED.h>
// Initialize the display with the follow pin connections.
SSD1320 flexibleOLED(10, 9); //10 = CS, 9 = RES
// Define constants for home and away team.
const int Home = A1;
const int Away = A2;
// Define constants for LED pins.
const int LED_Away = 7; // Pin 7 connected to a LED, turns HIGH when away team score.
const int LED_Home = 8; // Pin 8 connected to a LED, turns HIGH when home team score.
enum { Off = HIGH, On = LOW };
#define Interval 1000
// Initalize and define states for the goal sensors.
int currentHomeState = 0, previousHomeState = 0;
int currentAwayState = 0, previousAwayState = 0;
int homeGoals = 0; // introduce seperate variables for home and away goals
int awayGoals = 0;
//int numberOfGoals = 0;
unsigned long msecLst = 0;
void setup() {
//Run once
//Initilize the display
flexibleOLED.begin(160, 32); //Display is 160 wide, 32 high
flexibleOLED.clearDisplay(); //Clear display and buffer
//Display Home score
flexibleOLED.setContrast(255);
flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
flexibleOLED.setCursor(45, 7);
flexibleOLED.print(homeGoals);
//Display Away score
flexibleOLED.setContrast(255);
flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
flexibleOLED.setCursor(95, 7);
flexibleOLED.print(awayGoals);
flexibleOLED.display();
pinMode(Home, INPUT_PULLUP);
pinMode(Away, INPUT_PULLUP);
pinMode(LED_home, OUTPUT);
pinMode(LED_away, OUTPUT);
Serial.begin(9600);
}
void loop() {
unsigned long msec = millis ();
//Run repeatedly
currentHomeState = digitalRead(Home);
currentAwayState = digitalRead(Away);
//-------------handle home --------------------------------
if (currentHomeState != previousHomeState) {
previousHomeState = currentHomeState;
if (currentHomeState == On) {
if ( (msec - msecLst) > Interval) {
msecLst = msec;
homeGoals++;
Serial.println (homeGoals);
flexibleOLED.setContrast(255);
flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
flexibleOLED.setCursor(45, 6);
flexibleOLED.print(homeGoals);
flexibleOLED.display();
digitalWrite(LED_1, HIGH);
delay(250);
digitalWrite(LED_1, LOW);
}
if (homeGoals == 5) {
digitalWrite(LED_home, HIGH);
delay(250);
digitalWrite(LED_home, LOW);
delay(250);
digitalWrite(LED_home, HIGH);
delay(250);
digitalWrite(LED_home, LOW);
delay(250);
digitalWrite(LED_home, HIGH);
delay(250);
digitalWrite(LED_home, LOW);
delay(250);
digitalWrite(LED_home, HIGH);
delay(250);
digitalWrite(LED_home, LOW);
delay(250);
digitalWrite(LED_home, HIGH);
}
if (homeGoals == 5) {
homeGoals = 0;
awayGoals = 0;
delay(250);
digitalWrite(LED_home, LOW);
}
if (homeGoals == 0) {
flexibleOLED.clearDisplay(); // Clear display and buffer
flexibleOLED.setContrast(255);
flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
flexibleOLED.setCursor(45, 6);
flexibleOLED.print(homeGoals);
flexibleOLED.display();
Serial.println (0);
}
//----------------handle away
if (currentAwayState != previousAwayState) {
previousAwayState = currentAwayState;
if (currentAwayState == On) {
if ( (msec - msecLst) > Interval) {
msecLst = msec;
awayGoals++;
Serial.println (awayGoals);
flexibleOLED.setContrast(255);
flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
flexibleOLED.setCursor(45, 6);
flexibleOLED.print(awayGoals);
flexibleOLED.display();
digitalWrite(LED_away, HIGH);
delay(250);
digitalWrite(LED_away, LOW);
}
if (awayGoals == 5) {
digitalWrite(LED_away, HIGH);
delay(250);
digitalWrite(LED_away, LOW);
delay(250);
digitalWrite(LED_away, HIGH);
delay(250);
digitalWrite(LED_away, LOW);
delay(250);
digitalWrite(LED_away, HIGH);
delay(250);
digitalWrite(LED_away, LOW);
delay(250);
digitalWrite(LED_away, HIGH);
delay(250);
digitalWrite(LED_away, LOW);
delay(250);
digitalWrite(LED_away, HIGH);
}
if (awayGoals == 5) {
homeGoals = 0;
awayGoals = 0;
delay(250);
digitalWrite(LED_away, LOW);
}
if (awayGoals == 0) {
flexibleOLED.clearDisplay(); // Clear display and buffer
flexibleOLED.setContrast(255);
flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
flexibleOLED.setCursor(45, 6);
flexibleOLED.print(awayGoals);
flexibleOLED.display();
Serial.println (0);
}
}
}
}
Note that I did not check the code for errors. Also in general you should consider to write seperate functions to make the code easier to read.
Related
So i have a dht11 sensor connected to show the temperature and a HC-SR04 sensor to make a LED light up when the distance is under 15cm. I also have photoresistor to light up when it's dark and not ligh up when it's day.
I can do this on my own but the thing is i am trying to make the pushbutton as a toggle switch. When pressed once the sensors should stop printing data to the seriall monitor and the lights to turn off. When pressed again it shoul go back to printing the temperature, HC sensor and photoresistor should also work. When pressed again it should go back to stopping everything and so on.
The # is in norwegian don't mind that just explains the code
// #Include tar med en biblotek
// #define betyr at du gir navn til en constant integer
#include "DHT.h"
#define DHTTYPE DHT11
const int DHTPIN = 7; // fyll inn: digital pin sensoren er koblet til
DHT dht(DHTPIN, DHTTYPE);
const int trig = 10;
const int echo = 11;
int bstate = 1;
int led = 2; //En variabel for led pin.
float tid, avstand, t; // "t" en variabel for temperaturen
int led1 = 8;
int bpin = 5;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
pinMode(led1, OUTPUT);
pinMode(led, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(bpin, INPUT);
}
void loop() {
if(digitalRead(bpin) == LOW){
Serial.println("System on!");
bpin = 1;
while(bstate == 1){
//Temperatur Sensor kode:
t = dht.readTemperature(); //leser av temp verdier fra sensor og legger verdi inn i "t" variabelen.
Serial.print("Temperature: ");
Serial.println(t);
delay(2000);
return;
//Photoresistor LED
int verdi = analogRead(A1);
Serial.print("Verdi: ");
Serial.println(verdi);
if (verdi > 400){
digitalWrite(led1, HIGH);
}
else {
digitalWrite(led1, LOW);
}
if(digitalRead(bpin) == LOW)
{
bstate = 0;
Serial.println("System off!");
delay(1000);
}
//Ultralyd sensor kode:
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
tid = pulseIn(echo, HIGH);
avstand = (tid*0.034)/2;
if (avstand < 15){
digitalWrite(led, HIGH);
delay(10000);
}
else {
digitalWrite(led, LOW);
}
delay(500);
if(digitalRead(bpin) == LOW)
{
bstate = 0;
Serial.println("System off!");
delay(1000);
}
}
}
}
Another code with toggle switch:
#include "DHT.h"
#define DHTTYPE DHT11
const int DHTPIN = 7; // fyll inn: digital pin sensoren er koblet til
DHT dht(DHTPIN, DHTTYPE);
const int trig = 10;
const int echo = 11;
int ledstate = 0;
int bstate = 0;
int led = 2;
int led1 = 8;
int bpin = 5;
int bnew;
int bold = 1;
float tid, avstand, t;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(bpin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(bnew);
bnew = digitalRead(bpin);
if(bold == 0 && bnew == 1){
if(bstate == 0){
Serial.println("System on");
//Temperatur Sensor kode:
t = dht.readTemperature(); //leser av temp verdier fra sensor og legger verdi inn i "t" variabelen.
Serial.print("Temperature: ");
Serial.println(t);
delay(2000);
return;
//Photoresistor LED
int verdi = analogRead(A1);
Serial.print("Verdi: ");
Serial.println(verdi);
if (verdi > 400){
digitalWrite(led1, HIGH);
}
else {
digitalWrite(led1, LOW);
}
//Ultralyd sensor kode:
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
tid = pulseIn(echo, HIGH);
avstand = (tid*0.034)/2;
if (avstand < 15){
digitalWrite(led, HIGH);
delay(10000);
}
else {
digitalWrite(led, LOW);
}
delay(500);
bstate = 1;
}
else if(bstate == 1 && bnew == 1){
Serial.println("System off");
bstate = 0;
}
delay(1000);
}
bold = bnew;
delay(1000);
}
Code without if (Not toggle switch):
// C++ code
//
#include "DHT.h"
#define DHTTYPE DHT11
const int DHTPIN = 7; // fyll inn: digital pin sensoren er koblet til
DHT dht(DHTPIN, DHTTYPE);
const int trig = 10;
const int echo = 11;
int led = 2;
int led1 = 8;
float tid, avstand, t;
void setup()
{
pinMode(led, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}
void loop()
{
//Ultralyd sensor kode:
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
tid = pulseIn(echo, HIGH);
avstand = (tid*0.034)/2;
if (avstand < 15){
digitalWrite(led, HIGH);
delay(10000);
}
else {
digitalWrite(led, LOW);
}
delay(1500);
//Temperatur Sensor kode:
t = dht.readTemperature(); //leser av temp verdier fra sensor og legger verdi inn i "t" variabelen.
Serial.print("Temperature: ");
Serial.println(t);
delay(2000);
return;
//Photoresistor LED
int verdi = analogRead(A1);
Serial.print("Verdi: ");
Serial.println(verdi);
if (verdi > 400){
digitalWrite(led1, HIGH);
}
else {
digitalWrite(led1, LOW);
}
}
The connections are correct but when ii run these codes almost nothing happens. the first code i can turn system on but not off and just dht11 sensor is working, syste turned off automatic. The second code the temperature printed nan or - all the time and system on to other sensors did not work. The third code temperature kinda worked and HC-SR04 sensor worked but not the photoresistor.
Circuit drawing of curcuit
Scematic view of circuit
Anything would help thanks
So I am very new to this IoT stuff and what I am trying to create here is somewhat like traffic violation detection.
My idea is: when the red light is on and if the PIR sensor detects movement, the buzzer/LED turns on.
Here's the image:
Here's what the code looks like:
int pir = 2;
int rojo = 12;
int amarillo = 11;
int verde = 10;
int led = 7;
void setup() {
pinMode(pir, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
pinMode(verde, OUTPUT); // It declares the green pin as output
pinMode(amarillo, OUTPUT);// It declares the yellow pin as output
pinMode(rojo, OUTPUT);
}
void loop() {
digitalWrite(verde, HIGH); // It turns on the green led
delay(15000); //wait 15 seconds
digitalWrite(verde, LOW); // It turns off the green led
delay(250); //wait 0.25 seconds
digitalWrite(amarillo, HIGH); // It turns on the yellow led
delay(3000); //wait 3 seconds
digitalWrite(amarillo, LOW); // It turns off the yellow led
delay(250); //wait 0.25 seconds
int val = digitalRead(pir);
Serial.println(val);
digitalWrite(rojo, HIGH); // It turns on the red led
delay(15000); //wait 15 seconds
digitalWrite(rojo, LOW);
if (rojo == HIGH) {
if (val == HIGH) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
delay(10);
}
}
The problem is here:
digitalWrite(rojo, HIGH); //It turns on the red led
delay(15000); //wait 15 seconds
digitalWrite(rojo, LOW);
if (rojo == HIGH) {
if (val == HIGH) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
delay(10);
}
First of all, rojo is a pin number, not a value you want to use in this compare.
Second, during your delay delay(15000), the code stops running, so movement is not detected during this time.
The only way to detect during the 15s delay is by using millis() for your timing and delay (or using an interrupt).
You could try something like this (untested):
digitalWrite(rojo, HIGH); //It turns on the red led
unsigned long int redStartTime = millis();
while (millis() - redStartTime <= 15000) {
delay(100);
int val = digitalRead(pir);
if (val == HIGH) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
}
digitalWrite(rojo, LOW);
I didn't test this, but you get the idea.
Note that I don't know if the motion detector returns HIGH or LOW when something moves; you may need to change the code there.
You need to change the position of your if
int pir=2;
int rojo=12;
int amarillo=11;
int verde=10;
int led=7;
void setup()
{
pinMode(pir,INPUT);
pinMode(led,OUTPUT);
Serial.begin(9600);
pinMode(verde,OUTPUT); //It declares the green pin as output
pinMode(amarillo,OUTPUT);//It declares the yellow pin as output
pinMode(rojo,OUTPUT);
}
void loop()
{
digitalWrite(verde,HIGH); //It turns on the green led
delay(15000); //wait 15 seconds
digitalWrite(verde,LOW); //It turns off the green led
delay(250); //wait 0.25 seconds
digitalWrite(amarillo,HIGH); //It turns on the yellow led
delay(3000); //wait 3 seconds
digitalWrite(amarillo,LOW); //It turns off the yellow led
delay(250); //wait 0.25 seconds
int val = digitalRead(pir);
Serial.println(val);
digitalWrite(rojo,HIGH); //It turns the red led
if(val==HIGH){ //---> //I dont know if this value can be compared with HIGH, on my programm whe costum use numbers!
digitalWrite(led,HIGH);
}
else{
digitalWrite(led,LOW);
}
delay(15000); //wait 15 seconds
digitalWrite(rojo,LOW);
delay(10);
}
Hello i have problem :
state sensor = sensor steps "IF" anyone "steps now = impulse " = all code run"perfect good work im happy" but if sensor LOW a did not detect any motion while loading the code = broke stop all , even if it is at the end close or halfway, it will stop when there is no traffic "" (stage_sensor == HIGH)"", i dont have idea how to fix this//
Please help or sugestion
EDIT : There may be syntax errors (is not consistent), but just wanted to present a problem, the system only works under "if" and this is problem, i dont know how to fix
#include <AccelStepper.h> //accelstepper library
// #define light
// #define move_sensor
// #define pump_water
// #define fan_blowing
// #define fan_extractor
// #define blue
// #define red
// #define green
const byte limitSwitch_1 = 26;
const byte limitSwitch_2 = 25;
bool switchFlipped = false;
bool previousFlip = true;
int switchCounter;
int newSpeed;
int state_sensor = 0; // <--- stage sensor //
int light = 2;
int blue = 3;
int red = 4;
int green = 5;
int fan_blowing = 6;
int water_pump = 8;
int move_sensor = 9;
int fan_extractor = 10;
AccelStepper stepper(1, 22, 23);
void setup()
{
pinMode(water_pump, OUTPUT);
pinMode(light, OUTPUT);
pinMode(fan_blowing, OUTPUT);
pinMode(fan_extractor, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(move_sensor, INPUT);
pinMode(limitSwitch_1, INPUT_PULLUP); //pin 1 engine (IF touch)
pinMode(limitSwitch_2, INPUT_PULLUP); //pin 2 engine (IF touch)
Serial.begin(9600);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(100);
stepper.setSpeed(1000);
delay(500);
}
void loop()
{
digitalWrite(light, HIGH);
digitalWrite(blue, HIGH);
state_sensor = digitalRead(sensor_move);
if (state_sensor == HIGH) // <--- stage sensor IF anyone move = all code run but if sensor LOW did not detect movement all code broke stop all //
{
digitalWrite(blue, LOW);
digitalWrite(red, HIGH);
stepper.runSpeed();
engine();
}
}
void engine()
{
if(digitalRead(limitSwitch_1) == LOW)
{
switchCounter++;
delay(1000);
newSpeed = -1 * (1000 + (switchCounter * 200));
stepper.setSpeed(newSpeed);
}
if(digitalRead(limitSwitch_2) == LOW)
{
switchCounter++;
delay(1000);
newSpeed = -1 * (1000 + (switchCounter * 200));
stepper.stop();
fans();
}
}
void fans()
{
digitalWrite(red, HIGH);
{
digitalWrite(fan_blowing, HIGH);
digitalWrite(fan_extractor, HIGH);
delay(1000);
digitalWrite(water_pump, HIGH);
}
delay(1000);
digitalWrite(red, LOW);
digitalWrite(water_pump, LOW);
digitalWrite(green, HIGH);
delay(1000);
digitalWrite(fan_blowing, LOW);
digitalWrite(fan_extractor, LOW);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH); //this blue RGB light "ON" but if sensor_steps "ON" = active cycle , blue light led off
delay(1000);
}
What you could do is set up a global "stage" variable:
#include <AccelStepper.h> //accelstepper library
const byte limitSwitch_1 = 26;
const byte limitSwitch_2 = 25;
bool switchFlipped = false;
bool previousFlip = true;
int switchCounter;
int newSpeed;
int stage;
AccelStepper stepper(1, 22, 23);
void setup()
{
pinMode(limitSwitch_1, INPUT_PULLUP);
pinMode(limitSwitch_2, INPUT_PULLUP);
Serial.begin(9600);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(100);
stepper.setSpeed(1000);
delay(500);
stage = 0;
}
void loop()
{
stepper.runSpeed();
runner();
}
void runner()
{
if(stage == 0 && digitalRead(limitSwitch_1) == LOW)
{
switchCounter++;
delay(5000);
newSpeed = -1 * (1000 + (switchCounter * 200));
stepper.setSpeed(newSpeed);
++stage;
}
if(stage == 1 && digitalRead(limitSwitch_2) == LOW)
{
stepper.setSpeed(1000);
delay(1000);
stepper.setSpeed(0);
++stage;
}
if (stage == 2) {
// do next thing, then ++stage;, etc.
}
}
I recently bought an ELEGO Mega 2560, or in other words an Arduino Mega. I bought a bmp180 sensor as well. I connected the bmp in this fashion, VCC - 3.3v, GND - GND, SCL - 21, SDA - 20. I uploaded a simple code which just displayes altitude. When I go to the Serial Monitor to view the results, nothing pops up. It is suppose to say BMP init success if it connects, and fail if it doesn't. When I go to the monitor, it just doens't say anything. When I disconnect the sensor, it says fail. It appears as if the Serial Monitor just freezes. Also a headsup, my code is very messy, I'm sorry if it's hard to keep up.
#include <Wire.h>
#include <SFE_BMP180.h>
SFE_BMP180 bmp180;
float Po = 1014.9;
#define ledPin 7
#define TransmitPin 5
//int Altitude = 5;
int sendValue;
String incomingString;
unsigned long lastTransmission;
const int interval = 1000;
void setup() {
Wire.begin();
pinMode(ledPin, OUTPUT);
pinMode(2, INPUT);
pinMode(10, OUTPUT);
pinMode(TransmitPin, OUTPUT);
bool success = bmp180.begin();
Serial.begin(115200);
if (success) {
Serial.println("BMP180 init success");
}
else
Serial.println("fail");
}
void loop() {
sendValue = digitalRead(29);
if (sendValue == HIGH) {
if (millis() > lastTransmission + interval) {
Serial.println("AT+SEND=1,8,Return");
digitalWrite(TransmitPin, HIGH);
delay(100);
digitalWrite(TransmitPin, LOW);
lastTransmission = millis();
}
}
if (Serial.available()) {
incomingString = Serial.readString();
if (incomingString.indexOf("Testing!") > 0) {
digitalWrite(10, HIGH);
delay(100);
digitalWrite(10, LOW);
}
}
char status;
double T, P, alt;
bool success = false;
status = bmp180.startTemperature();
if (status != 0) {
delay(1000);
status = bmp180.getTemperature(T);
if (status != 0) {
status = bmp180.startPressure(3);
if (status != 0) {
delay(status);
status = bmp180.getPressure(P, T);
if (status != 0) {
if (millis() > lastTransmission + interval) {
alt = bmp180.altitude(P, Po);
Serial.print("AT+SEND=1,8,");
int altAsFoot = alt * 3.281;
Serial.println(altAsFoot);
digitalWrite(TransmitPin, HIGH);
delay(100);
digitalWrite(TransmitPin, LOW);
}
for (int i = 0; i < 1800; i++) {
delay(1);
if (Serial.available()) {
incomingString = Serial.readString();
if (incomingString.indexOf("+OK") > 0) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
}
if (incomingString.indexOf("Testing!") > 0) {
digitalWrite(10, HIGH);
delay(100);
digitalWrite(10, LOW);
}
}
}
}
}
}
}
}
Turns out it was a hardware issue. I had ground shorted to SDA. I'm assuming the same will happen if it's shorted to SCL. Make sure both SDA and SCL aren't shorted to each other or ground.
What I wanted to do is to run 2 traffic lights simultaneously(ex. North and South).Is there an alternative for delay besides millis? I tried to use Blinkwithoutdelay but as a newbie its very complicated for me to use it.
digitalWrite(greenled, HIGH); //Green on for 1 seconds
delay(greenDuration);
digitalWrite(greenled, LOW); //Green off, yellow on for 1 seconds
digitalWrite(yellowled, HIGH);
delay(1000);
digitalWrite(yellowled, LOW); //yellow off, red on for 1 seconds
digitalWrite(redled, HIGH);
delay(1000);
digitalWrite(redled, LOW); //Red off
digitalWrite(greenled2, HIGH); //Green on for 1 seconds
delay(1000);
digitalWrite(greenled2, LOW); //Green off, yellow on for 1 seconds
digitalWrite(yellowled2, HIGH);
delay(1000);
digitalWrite(yellowled2, LOW); //yellow off, red on for 1 seconds
digitalWrite(redled2, HIGH);
delay(1000);
digitalWrite(redled2, LOW); //Red off
Complete code:
int beam = 2;//Beam sensor
int greenled = 4;
int redled = 7;
int yellowled = 13;
int greenDuration =1000; //normal time
int greenShortDuration = 1000;
int greenIncrement = 5000; //5 seconds
void setup()
{
// set the digital pin as output:
pinMode(greenled, OUTPUT);
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(beam,INPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println("Status:low");
digitalWrite(greenled, HIGH); //Green on for 5 seconds
delay(greenDuration);
digitalWrite(greenled, LOW); //Green off, yellow on for 2 seconds
digitalWrite(yellowled, HIGH);
delay(1000);
digitalWrite(yellowled, LOW); //yellow off, red on for 5 seconds
digitalWrite(redled, HIGH);
delay(1000);
digitalWrite(redled, LOW); //Red and Yellow off
digitalWrite(greenled2, HIGH); //Green on for 1 seconds
delay(1000);
digitalWrite(greenled2, LOW); //Green off, yellow on for 1 seconds
digitalWrite(yellowled2, HIGH);
delay(1000);
digitalWrite(yellowled2, LOW); //yellow off, red on for 1 seconds
digitalWrite(redled2, HIGH);
delay(1000);
digitalWrite(redled2, LOW); //Red off
if(digitalRead(beam)==HIGH){
for(int i=1; i<=10; i++){
Serial.println(i);
while(i>=10){
Serial.println("Motion Detected");
greenDuration +=greenIncrement; //Add 5 seconds everytime
Serial.println(greenDuration);
break;
}
}
}
if(!digitalRead(beam)==HIGH){
Serial.println("hey"); //indication of code is working
greenDuration=greenShortDuration;
return;
}
}
I tried to use Blinkwithoutdelay but as a newbie its very complicated
for me to use it.
You have to try harder :)
Imagine loop() isn't the "program" but a description of any moment.
In general it detects that neither time for a change nor a new pushbutton request has come, so loop exits immediately.
Even if something should have happened, any new reaction does not take any time, and loop exits immediately.
There is a simple solution to solve the given traffic lights sample.
Instead of calling the delay function you could call your special delay-function:
int globalFlagSomeonePressedBeam = false; // store global if someone pushed the beam button
void delay_and_check_for_beam (int msDelay) {
for (int i=0; i<msDelay; ++i) {
if(digitalRead(beam)==HIGH){ // check beam button
globalFlagSomeonePressedBeam = true; // store information
}
delay(1);
}
}
Between the calls to your delay function you set the right lights (digital outs)
After you check your globalFlagSomeonePressedBeam you need to set it back to false
Solution by OP.
unsigned long curGoStopDuration, defGoStopDuration = 5000, maxGoStopDuration = 30000;
unsigned long currentMillis;
#pragma region SensorFields
int durationIncrement = 2500;
unsigned long greenPrevMillis;
float DetectionTime = 2500;
bool triggered;
#pragma endregion
enum LightState { Green, Yellow, Red };
class Stoplight
{
private:
int greenLed, yellowLed, redLed;
unsigned long previousMillis;
float CurInterval;
LightState state;
public:
unsigned long GreenDuration = 5000;
unsigned long RedDuration = 5000;
int YellowDuration = 1000;
Stoplight(int gLed, int yLed, int rLed) :greenLed(gLed), yellowLed(yLed), redLed(rLed)
{
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
}
void Loop()
{
if (currentMillis - previousMillis >= CurInterval)
{
previousMillis = currentMillis;
//Transitions
switch (state)
{
case Green:
ToYellow();
break;
case Yellow:
ToRed();
break;
case Red:
ToGreen();
break;
}
}
}
void ToGreen()
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
CurInterval = GreenDuration - YellowDuration;
state = Green;
}
void ToYellow()
{
digitalWrite(greenLed, LOW);
digitalWrite(yellowLed, HIGH);
CurInterval = YellowDuration;
state = Yellow;
}
void ToRed()
{
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, HIGH);
CurInterval = RedDuration;
state = Red;
}
};
Stoplight SL_01(2, 3, 4), SL_02(5, 6, 7);
int beamSensor = 8;
void setup()
{
//Set default values
curGoStopDuration = defGoStopDuration;
SL_01.ToGreen();
SL_02.ToRed();
pinMode(beamSensor, INPUT);
Serial.begin(9600);
}
void loop()
{
currentMillis = millis();
Sensor();
//Manipulate durations
SL_01.GreenDuration = curGoStopDuration;
SL_02.RedDuration = curGoStopDuration;
SL_01.Loop();
SL_02.Loop();
}
void Sensor()
{
//If sensor detects something
if (digitalRead(beamSensor) == HIGH)
{
if (!triggered)
{
greenPrevMillis = currentMillis;
triggered = true;
}
if (currentMillis - greenPrevMillis >= DetectionTime)
{
if(curGoStopDuration < maxGoStopDuration)
curGoStopDuration += durationIncrement; //Add seconds
greenPrevMillis = currentMillis;
Serial.print("Green light duration is now: ");
Serial.println(curGoStopDuration);
}
}
else //No detection
{
curGoStopDuration = defGoStopDuration;
triggered = false;
}
}
You need interrupt on your beam, fast one, just counter, but its necessary for more precise counting (traffic =) )... this works for me just fine:
#define redPin1 40
#define yellowPin1 41
#define greenPin1 42
#define beamPin1 21
#define redDuration 1000
#define yellowDuration 1000
#define greenDuration 1000
#define durationIncrement 500
byte carCounter1 = 0;
byte state1 = 1;
void setup()
{
pinMode(redPin1, OUTPUT);
pinMode(yellowPin1, OUTPUT);
pinMode(greenPin1, OUTPUT);
pinMode(beamPin1, INPUT);
attachInterrupt(digitalPinToInterrupt(beamPin1), beamCount1, RISING);
}
void beamCount1()
{
carCounter1++;
}
void wait(long duration)
{
int wait = 0;
long last = millis();
while (wait == 0)
{
long now = millis();
if (now - last >= duration) { wait = 1;}
else {}
}
}
void redLight(byte redLightPin)
{
digitalWrite(redLightPin, HIGH);
wait(redDuration);
state1 = 2;
}
void yellowLight1(byte redLightPin, byte yellowLightPin)
{
digitalWrite(yellowLightPin, HIGH);
wait(yellowDuration);
digitalWrite(redLightPin, LOW);
digitalWrite(yellowLightPin, LOW);
state1 = 3;
}
void yellowLight2(byte yellowLightPin)
{
digitalWrite(yellowLightPin, HIGH);
wait(yellowDuration);
digitalWrite(yellowLightPin, LOW);
state1 = 1;
}
void greenLight(byte greenLightPin)
{
digitalWrite(greenLightPin, HIGH);
long incremented = greenDuration + (carCounter1 * durationIncrement);
wait(incremented);
digitalWrite(greenLightPin, LOW);
carCounter1 = 0;
state1 = 4;
}
void trafficLight(byte red, byte yellow, byte green)
{
switch (state1){
case 1:
redLight(red);
break;
case 2:
yellowLight1(red, yellow);
break;
case 3:
greenLight(green);
break;
case 4:
yellowLight2(yellow);
break;
}
}
void loop()
{
trafficLight(redPin1, yellowPin1, greenPin1);
}
no delays, just wait function which makes track of millis... if you want more precision, count the time needed for each light and remove it from wait...