I'm currently working on a small Arduino project, and I'm kinda stuck. Here's my code so far:
#include "_init.h"
void setup() {
ds_init();
Serial.begin(9600);
randomSeed(analogRead(LIGHT_SENSOR_PIN));
int rol1[] = {1, 2, 3, 4, 5};
int rol2[] = {3, 5, 1, 2, 4};
int rol3[] = {5, 1, 4, 3, 2};
int rol1Mid = random(0, 5);
int rol1Bov = rol1Mid + 1;
int rol1Ond = rol1Mid - 1;
if (rol1Bov == 5){
rol1Bov = 0;
}
if (rol1Ond == -1){
rol1Ond = 4;
}
int rol2Mid = random(0, 5);
int rol2Bov = rol2Mid + 1;
int rol2Ond = rol2Mid - 1;
if (rol2Bov == 5){
rol2Bov = 0;
}
if (rol2Ond == -1){
rol2Ond = 4;
}
int rol3Mid = random(0, 5);
int rol3Bov = rol3Mid + 1;
int rol3Ond = rol3Mid - 1;
if (rol3Bov == 5){
rol3Bov = 0;
}
if (rol3Ond == -1){
rol3Ond = 4;
}
Serial.println(rol1[rol1Bov]);
Serial.println(rol1[rol1Mid]);
Serial.println(rol1[rol1Ond]);
Serial.println("----");
Serial.println(rol2[rol2Bov]);
Serial.println(rol2[rol2Mid]);
Serial.println(rol2[rol2Ond]);
Serial.println("----");
Serial.println(rol3[rol3Bov]);
Serial.println(rol3[rol3Mid]);
Serial.println(rol3[rol3Ond]);
if(rol1[rol1Mid] && rol2[rol2Mid] == rol3[rol3Mid]){
Serial.println("Yaay!");
} else {
Serial.println("Jammer, probeer het nogmaals.");
}
/*
if(rol1[positieBov] && rol2[positieBov] == rol3[positieBov]){
Serial.println("Yaay!");
} else {
Serial.println("Jammer, probeer het nogmaals.");
}
if(rol1[positieOnd] && rol2[positieOnd] == rol3[positieOnd]){
Serial.println("Yaay!");
} else {
Serial.println("Jammer, probeer het nogmaals.");
}
*/
}
void loop() {
// put your main code here, to run repeatedly:
}
Basically, I'm trying to create a slot machine. There's probably tons of things wrong with my code, but for the time being, I'm stuck on the if statement that checks if the user won or not.
I know I shouldnt be coding in the setup() part of Arduino, but as soon as this all works, I'm going to put it in a function in the loop(), where I will use my dangershield's buttons to operate the three roll's separately.
Okay, managed to fix it... I feel silly now
if(rol1[rol1Mid] == rol2[rol2Mid] && rol2[rol2Mid] == rol3[rol3Mid])
Related
why is it like this How should I fix it?
It's my first time using PMS5003 with 7segment
I am able to display the value in the Serial Monitor but cannot get the pm2_5 value to be displayed in the 7 segment.
#include "SevSeg.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(A0, A1);
SevSeg sevseg;
unsigned int pm1 = 0;
unsigned int pm2_5 = 0;
unsigned int pm10 = 0;
void setup() {
Serial.begin(9600);
while (!Serial) ;
mySerial.begin(9600);
byte numDigits = 4;
byte digitPins[] = {10, 11, 12, 13};
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; //6, 7, 8, 9, 10, 11, 12, 13
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_CATHODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(30);
}
void loop() {
int index = 0;
char value;
char previousValue ;
static unsigned long timer = millis ();
static int deciSeconds = 0;
if (millis() - timer >= 100)
{
timer += 100;
sevseg.setNumber(pm2_5, 2);
}
sevseg.refreshDisplay();
while (mySerial.available()) {
value = mySerial.read();
if ((index == 0 && value != 0x42) || (index == 1 && value != 0x4d)) { //0x42,0x4d ค่าคงที่
Serial.println("Cannot find the data header.");
break;
}
if (index == 4 || index == 6 || index == 8 || index == 10 || index == 12 || index == 14) {
previousValue = value;
}
else if (index == 5) {
pm1 = 256 * previousValue + value;
Serial.print("{ ");
Serial.print("\"pm1\": ");
Serial.print(pm1);
Serial.print(" ug/m3");
Serial.print(", ");
}
else if (index == 7) {
pm2_5 = 256 * previousValue + value;
Serial.print("\"pm2_5\": ");
Serial.print(pm2_5);
Serial.print(" ug/m3");
Serial.print(", ");
}
else if (index == 9) {
pm10 = 256 * previousValue + value;
Serial.print("\"pm10\": ");
Serial.print(pm10);
Serial.print(" ug/m3");
} else if (index > 15) {
break;
}
index++;
}
while (mySerial.available()) mySerial.read();
Serial.println(" }");
delay(1000);
}
I want the 7segment to be able to display the value of the sensor PMS5003.
I'd like to get out of the loop in my Arduino project. Currently I am programming a digital watch, everything works just fine but I wanted to add options menu by clicking a button, but after clicking it nothing pops up even if I have something inside the code. Take a look at it. I don't know how to write it better. If you have some ideas, please you could rewrite some parts of the code and explain why you did so. Thanks forwardly.
Please do not mind s = s + 1, I wanted it like that.
#include "LiquidCrystal.h"
#include <EEPROM.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int h = 0;
int m = 0;
int s = 0;
int right = 8;
int left = 9;
int buttonStateLeft = 0;
String when;
uint8_t EEPROMaddress_sec = 1;
uint8_t EEPROMaddress_min = 2;
uint8_t EEPROMaddress_hour = 3;
bool clockShown = true;
bool menuShown = false;
void setup()
{
lcd.begin(16,2);
pinMode(right, INPUT);
pinMode(left, INPUT);
}
void loop()
{
if(menuShown)
{
lcd.setCursor(0,0);
lcd.print("jozo je kkt");
delay(200);
}
if(clockShown) {
lcd.setCursor(0,0);
buttonStateLeft = digitalRead(left);
if(buttonStateLeft == HIGH)
{
clockShown = false;
menuShown = true;
lcd.clear();
}
s = EEPROM.read(EEPROMaddress_sec);
m = EEPROM.read(EEPROMaddress_min);
h = EEPROM.read(EEPROMaddress_hour);
s = s + 1;
if(h > 12)
when = "PM";
if(h < 12)
when = "AM";
if(h == 12)
when = "PM";
lcd.print("Cas: ");
if(h<10)lcd.print("0");
lcd.print(h);
lcd.print(":");
if(m<10)lcd.print("0");
lcd.print(m);
lcd.print(":");
if(s<10)lcd.print("0");
lcd.print(s);
lcd.print(" ");
lcd.print(when);
if(s == 60)
{
s = 0;
m = m+1;
}
if(m == 60)
{
s = 0;
m = 0;
h = h+1;
}
if(h == 24)
{
m = 0;
s = 0;
h = 0;
}
EEPROM.write(EEPROMaddress_sec, s);
EEPROM.write(EEPROMaddress_min, m);
EEPROM.write(EEPROMaddress_hour, h);
delay(1000);
}
}
In order to do that you will have to use Interrupts, note that you must connect your button to an interrupt pin (not every pin is an interrupt pin) you can google "what are the interrupt pins of 'your_card_name' ", the code would have to change, you can follow these :
In the setup function replace :
pinMode(left, INPUT);
by :
attachInterrupt(digitalPinToInterrupt(left), switchMode, RISING);
add this function before setup(){...}
int lastPressTime=millis();
void switchMode(){ // function called when the button is pressed
if((millis()-lastPressTime)>60){ // for debouncing
clockShown = false;
menuShown = true;
lcd.clear();
lastPressTime=millis();
}
}
and remove this part from your code : (the one in the loop() function)
buttonStateLeft = digitalRead(left);
if(buttonStateLeft == HIGH)
{
clockShown = false;
menuShown = true;
lcd.clear();
}
I want to interface Arduino with PLC to pull some information.
My problem is at Function 3: Set alarm flag / reset flag. This function is used to compare history value and present value. I tried to process some integer number (test_number) and process like binary 16 bits data for finding 1 at some bit. I found the for loop in Findbit function, which should repeat 16 times, runs infinitely. It does not change the incremental index (variable name bit_1) which is still stuck at 1.
This is my code :
int test_number_array[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int test_number = 0;
int bit_1 = 0;
int Andbit = 0;
const char* message;
int flagAlarm[2][16] = {};
int flagReset[2][16] = {};
void setup()
{
// put your setup code here, to run once:
Serial.begin( 9600 );
}
void loop()
{
// put your main code here, to run repeatedly:
for (int j = 1; j <= 2; j++)
{
for (int i = 1; i <= 2; i++) // Example with 2 modbus address
{
unsigned int address = 40000 + i;
Serial.print ("Modbus address = ");
Serial.println(address, DEC);
pull_data(i);
Serial.print("Test number is ");
Serial.println(test_number);
Findbit(i);
Serial.println("------------------------------------------------- ");
}
}
while (1)
{
}
}
// ---------------Function 1 : Function finding alarm bit-----------------//
void Findbit(int i)
{
for (bit_1 = 0; bit_1 <= 15; bit_1++)
{
Andbit = test_number & 1;
Serial.print("Test number (BINARY) is ");
Serial.println(test_number, BIN);
Serial.print("Check at bit number ");
Serial.println(bit_1);
Serial.print("And bit is ");
Serial.println(Andbit, BIN);
Serial.print("flagAlarm(Before1) = ");
Serial.println(flagAlarm[i][bit_1]);
Serial.print("flagreset(Before1) = ");
Serial.println(flagReset[i][bit_1]);
if (Andbit == 1) //found "1" pass into loop
{
flagAlarm[i][bit_1] = 1;
}
else
{
}
Serial.print("flagAlarm(Before2) = ");
Serial.println(flagAlarm[i][bit_1]);
Serial.print("flagreset(Before2) = ");
Serial.println(flagReset[i][bit_1]);
Set_reset_flag(i,bit_1);
test_number = test_number >> 1;
Serial.print("flagAlarm(After) = ");
Serial.println(flagAlarm[i][bit_1]);
Serial.print("flagreset(After) = ");
Serial.println(flagReset[i][bit_1]);
Serial.println(" ");
}
}
// -----------------------Function 2 : Pull data------------------------- //
int pull_data(int i)
{
i = i - 1;
test_number = test_number_array[i];
return test_number;
}
// -------------Function 3 : Set alarm flag / reset flag ---------------- //
void Set_reset_flag(int i, int bit_1)
{
Serial.print("i = ");
Serial.println(i);
Serial.print("bit_1 = ");
Serial.println(bit_1);
if (flagAlarm[i][bit_1] == 1 && flagReset[i][bit_1] == 0)
{
Serial.print("Alarm at bit ");
Serial.println(bit_1);
flagAlarm[i][bit_1] = 0;
flagReset[i][bit_1] = 1;
}
else if (flagAlarm[i][bit_1] == 0 && flagReset[i][bit_1] == 1)
{
Serial.print("Reset Alarm at bit ");
Serial.println(bit_1);
flagReset[i][bit_1] = 0;
}
else if (flagAlarm[i][bit_1] == 1 && flagReset[i][bit_1] == 1)
{
Serial.print("Alarm still active at bit ");
Serial.println(bit_1);
flagAlarm[i][bit_1] = 0;
flagReset[i][bit_1] = 1;
}
else
{
}
}
Could it be that your bit_1 variable is modified from some other code not mentioned here, or get optimized at all? Also, is it necessary to make a loop counter a global variable? Can you declare it inside the Findbit function?
We're currently making a 4x3 calculator using Arduino and LCD. We're lacking buttons so instead of one button per operation, there's only one button for all operations. So far, it only does addition. How do you do the thing wherein if I pressed the OPERATION button once, it does addition, if twice, subtraction, etc.
#include <Keypad.h>
#include <LiquidCrystal.h> //import lcd library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //lcd pins
//LiquidCrystal lcd(5,4,3,2,1,0);
const byte ROWS = 4; // four rows
const byte COLS = 3;
//define the keymap
char keys [ROWS] [COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'+', '0', '='}
};
byte rowPins[ROWS] = {
9 ,8 ,7 ,6}; //connect keypad ROW1, ROW2, ROW3, ROW4 to these arduino pins
byte colPins[COLS] = {
13, 10, 1};
//create the keypad
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//variables declaration
boolean valOnePresent = false;
boolean next = false;
boolean final = false;
String num1, num2;
int ans;
char op;
void setup(){
lcd.begin(16,2);
lcd.setCursor(2,0);
lcd.print("Calculator");
delay(2500);
lcd.clear(); //clears the LCD screen and positions the cursor in the upper-left corner.
}
void loop(){
char key = myKeypad.getKey();
if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){
if (valOnePresent != true){
num1 = num1 + key;
int numLength = num1.length();
lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
lcd.print(num1);
}
else {
num2 = num2 + key;
int numLength = num2.length();
lcd.setCursor(15 - numLength, 1);
lcd.print(num2);
final = true;
}
}
else if (valOnePresent == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')){
if (valOnePresent == false){
valOnePresent = true;
op = key;
lcd.setCursor(15,0);
lcd.print(op);
}
}
else if (final == true && key != NO_KEY && key == '='){
if (op == '+')
{
ans = num1.toInt() + num2.toInt();
}
else if (op == '='){
ans = num1.toInt() + num2.toInt();
}
/* else if (op == '+')
{
answ = num1.toInt() - num2.toInt();
}
*/
lcd.clear();
lcd.setCursor(15,0);
lcd.autoscroll();
lcd.print(ans);
lcd.noAutoscroll();
}
}
You could use an array in order to accomplish this. By implementing a while loop with a small delay you can keep iterating through the positions in the array every time the button is pushed until the array times out. Here's an example of some could you could use to implement it.
char ops [4] = {'+','-','/','*'};
int del = 2500;
int strt = millis();
int location = 0;
while (millis() - strt < del) {
key = myKeypad.getkey();
if (key == '+') {
if (loc == 3) {
location = 0;
}
else {
location += 1;
}
strt = millis();
}
}
op = ops(location);
I'm having a problem with following program:
What it was supposed to do:
First child process should get a number from STDIN and set it to shared variable.
Second child process should read number from shared variable and print it binary on STDOUT.
What it does:
It never asks for a number, after running it, it just prints inifnite number of zero's. It looks like it jumps straight away into a second child and never leaves it.
Code:
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
/* jest zdefiniowane w sys/sem.h */
#else
union semun
{
int val;
struct semid_ds* buf;
unsigned short int* array;
struct seminfo* __buf;
};
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <fcntl.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/mman.h>
//shared
static int *glob_var;
struct sembuf operacja;
void operacjaSem(int, int, int);
int main(void)
{
/*---------------------------------------*/
//shared memory
glob_var = mmap(NULL, sizeof(*glob_var), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
/*---------------------------------------*/
//semafory
key_t klucz;
int semafor;
union semun ustaw;
//tworze jednoznaczny klucz dla semaforow
if((klucz = ftok(".", 'B')) == -1)
{
fprintf(stderr, "blad tworzenia klucza\\n");
exit(1);
}
//stworzenie semaforow
if((semafor = semget(klucz, 2, IPC_CREAT | IPC_EXCL | 0600)) == -1)
{
fprintf(stderr, "blad tworzenia semaforow\\n");
exit(1);
}
//inicjacja semaforow
ustaw.val = 1;
if(semctl(semafor, 0, SETVAL, ustaw) == -1)
{
fprintf(stderr, "blad ustawienia semafora0\\n");
exit(1);
}
ustaw.val = 0;
if(semctl(semafor, 1, SETVAL, ustaw) == -1)
{
fprintf(stderr, "blad ustawienia semafora1\\n");
exit(1);
}
if(fork() == 0)
{
//first child process
while(1)
{
if(semctl(semafor, 0, GETVAL) == 1)
{
//ustawiam zmienna wspoldzielona
char buf2[128];
fgets(buf2, 128, 0);
*glob_var = atoi(buf2);
//printf("buf: %d", *glob_var);
//przestawiam semafory
operacjaSem(semafor, 0, -1);
operacjaSem(semafor, 1, 1);
}
}
}
if(fork() == 0)
{
//second child process
while(1)
{
if(semctl(semafor, 1, GETVAL) == 1)
{
operacjaSem(semafor, 1, -1);
//odczytuje i wypisuje
int x = *glob_var;
int p[32];
for(int z = 0; z < 32; z++)
{
if(x % 2 == 1 || x % 2 == -1) p[z] = 1;
else p[z] = 0;
x = x >> 1;
}
for(int z = 31; z >= 0; z--)
printf("%d", p[z]);
printf("\n");
//przestawiam semafory
operacjaSem(semafor, 1, -1);
operacjaSem(semafor, 0, 1);
}
}
}
return 0;
}
void operacjaSem(int semafor, int nr, int op)
{
operacja.sem_num = nr;
operacja.sem_op = op;
operacja.sem_flg = 0;
if(semop(semafor, &operacja, 1) == -1)
{
fprintf(stderr, "blad operacji na semaforze\\n");
exit(1);
}
}
Change the first line that reads:
if(fork() == 0)
To something like
pid_t processID = fork();
if(processID == 0)
Change the second line that reads
if(fork() == 0)
To an else. More explicitly, for that second line, change:
}
if(fork() == 0)
{
//second child process
To:
}
else
{
if (processID < 0) {
printf ("Fork error\n"); exit(-1);
}
//second child process
And probably if the rest of your code is OK, it will start working. At least this will get your forks in good shape so you can keep working with the rest of the code.