Can I use the result of a multiplication of an array, for a function?

Hello! New to the forum and to Arduino, I am developing a toy for a project for school. Its a kids game that has a trigger that starts a "multiplication of an array (Possible Food Options). Then, Displays the "Result" on a LCD. I am trying to control LED's with the Possible "Result". But I am stuck, can anyone lend me a hand?

#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27
#define LCD_COLUMNS 16
#define LCD_LINES   2
#define BUTTON_PIN  7 //Trigger
#define BUTTON_PIN1 8 //Trigger
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
char *options[]={"Pie","Apple","Fish","Pizza"};
long option1;


void setup() {
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(31, INPUT); //Ignore for now
  pinMode(34, OUTPUT);//LED #1 (This is my backlight it will stay on, while no trigger has been set)
  pinMode(37, OUTPUT);//LED #2
  pinMode(38, OUTPUT);//LED #3
  lcd.init();
  lcd.backlight();
  randomSeed(4);
 Serial.begin(9600);
 Serial.print(options[option1]);
}
void loop() {
  int state=0;
 state=digitalRead(BUTTON_PIN),(BUTTON_PIN1);
 if(state==HIGH){

 option1 = random(sizeof(options)/sizeof(char*));
 lcd.clear();
 delay(800);
 lcd.print("PROCESSING....");
 lcd.blink();
 delay(4000);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("You Win:");
 lcd.setCursor(0,1);
 lcd.print(options[option1]);
 delay (4000);
 lcd.clear();
 }

 else{
 lcd.setCursor(0,0);
 lcd.print("Ready...");
 lcd.blink();
 delay(1000);
 }

  if(digitalRead(options[option1])=="Pie")
    {digitalWrite(37, HIGH);}
      {digitalWrite(34, HIGH);
      digitalWrite(37, LOW);
      digitalWrite(38, LOW);}
  if(digitalRead(options[option1])=="Apple")
    {digitalWrite(37, HIGH);}
      {digitalWrite(34, HIGH);
      digitalWrite(37, LOW);
      digitalWrite(38, LOW);}
  if(digitalRead(options[option1])=="Fish")
    {digitalWrite(38, HIGH);}
      {digitalWrite(34, HIGH);
      digitalWrite(37, LOW);
      digitalWrite(38, LOW);}
  if(digitalRead(options[option1])=="Pizza")
    {digitalWrite(38, HIGH);}
      {digitalWrite(34, HIGH);
      digitalWrite(37, LOW);
      digitalWrite(38, LOW);}
}

DigitalRead returns a boolean. It wil never be equal to pizza.

Thank you for the reply! Okay, Can i phrase it different then? or am I on the wrong path

Welcome to the forum

    if (digitalRead(options[option1]) == "Pie")

This code is a mess

  1. digitalRead() reads the state of a pin, ie HIGH or LOW, not a string
  2. You cannot use == to compare strings

I assume that you want to compare the string "Pie" with the option1 level of the array of pointers

If so, try this

    if (strcmp(options[option1], "Pie") == 0)

As an aside. Why is option1 defined as a long ? How many options are you hoping to allow for ?

Thank you for the reply! Thank you for the help.
1.) I believe its working but the LED is flashing for mil sec. How can I Add a duration in-between LED 1# Turning back on and LED 2/3 Turning off?

2.) Originally the randomSeed[] was 100. Should I leave it lower or higher? I need the result to be unpredictable

Please post your full modified sketch

I use the wokwi simulator, does this help? Sorry I am still learning the terminology.

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-mega", "id": "mega", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-lcd1602",
      "id": "lcd1",
      "top": 227.2,
      "left": 44,
      "attrs": { "pins": "i2c" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": 436.8,
      "left": 57.05,
      "rotate": 90,
      "attrs": { "value": "220000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": 436.8,
      "left": 172.25,
      "rotate": 90,
      "attrs": { "value": "220000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r4",
      "top": 436.8,
      "left": 297.05,
      "rotate": 90,
      "attrs": { "value": "220000" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 491.2,
      "left": 61.8,
      "rotate": 180,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 491.2,
      "left": 177,
      "rotate": 180,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": 491.2,
      "left": 301.8,
      "rotate": 180,
      "attrs": { "color": "red", "flip": "1" }
    },
    { "type": "wokwi-slide-switch", "id": "sw1", "top": -158.8, "left": 22.3, "attrs": {} },
    { "type": "wokwi-slide-switch", "id": "sw2", "top": -158.8, "left": 137.5, "attrs": {} },
    { "type": "wokwi-slide-switch", "id": "sw3", "top": -158.8, "left": 79.9, "attrs": {} },
    { "type": "wokwi-slide-switch", "id": "sw4", "top": -158.8, "left": 195.1, "attrs": {} },
    { "type": "wokwi-slide-switch", "id": "sw5", "top": -158.8, "left": 262.3, "attrs": {} }
  ],
  "connections": [
    [ "lcd1:GND", "mega:GND.2", "black", [ "h0", "v-67.2", "h124.8" ] ],
    [ "lcd1:VCC", "mega:5V", "red", [ "h0", "v-67.1", "h115.2" ] ],
    [ "bz2:1", "mega:GND.4", "green", [ "v0", "h-48", "v-182.4", "h211.2" ] ],
    [ "bz3:1", "mega:GND.4", "green", [ "v0", "h-48", "v-182.4", "h28.8" ] ],
    [ "bz2:2", "mega:37", "red", [ "v0", "h28.4", "v-172.8", "h124.8" ] ],
    [ "bz3:2", "mega:38", "red", [ "h38", "v-462.55" ] ],
    [ "r2:1", "mega:34", "red", [ "h0", "v-28.8", "h297.6" ] ],
    [ "r3:1", "mega:37", "red", [ "h0", "v-28.8", "h153.6" ] ],
    [ "r4:1", "mega:38", "red", [ "h-0.55", "v-27.95", "h48" ] ],
    [ "r2:2", "led1:A", "red", [ "h0" ] ],
    [ "r3:2", "led2:A", "red", [ "h0" ] ],
    [ "r4:2", "led3:A", "red", [ "h0" ] ],
    [ "led1:C", "mega:GND.4", "green", [ "v-124.8", "h278.4", "v-203.15" ] ],
    [ "led2:C", "mega:GND.4", "green", [ "v-124.8", "h163.2" ] ],
    [ "mega:GND.4", "led3:C", "green", [ "v203.15", "h-5.8", "v0", "h-38.4" ] ],
    [ "mega:GND.1", "sw1:1", "black", [ "v-9", "h-80.6" ] ],
    [ "sw1:1", "sw3:1", "black", [ "v0", "h-9.6", "v-48", "h48", "v48" ] ],
    [ "sw3:1", "sw2:1", "black", [ "v0", "h-19.2", "v-48", "h67.2", "v48" ] ],
    [ "sw2:1", "sw4:1", "black", [ "v0", "h-9.6", "v-48", "h57.6", "v48" ] ],
    [ "mega:5V.1", "sw5:3", "red", [ "v0" ] ],
    [ "sw4:3", "sw2:3", "red", [ "v0", "h9.8", "v-57.6", "h-57.6", "v57.6" ] ],
    [ "sw2:3", "sw3:3", "red", [ "v0", "h9.8", "v-57.6", "h-57.6", "v57.6" ] ],
    [ "sw3:3", "sw1:3", "red", [ "v0", "h9.8", "v-57.6", "h-57.6", "v57.6" ] ],
    [ "lcd1:SDA", "mega:20", "green", [ "h-9.6", "v-76.6", "h355.2", "v-211.2", "h-67.2" ] ],
    [
      "lcd1:SCL",
      "mega:21",
      "blue",
      [ "h-19.2", "v-76.5", "h374.4", "v-220.8", "h-57.6", "v-9.6" ]
    ],
    [ "sw1:2", "mega:8", "green", [ "v115.2", "h134.5" ] ],
    [ "sw3:2", "mega:8", "green", [ "v86.4", "h86.5" ] ],
    [ "sw2:2", "mega:8", "green", [ "v67.2", "h19.3", "v19.2" ] ],
    [ "sw4:2", "mega:8", "green", [ "v86.4", "h-38.3" ] ],
    [ "sw5:2", "mega:7", "green", [ "v115.2", "h-86.3", "v0", "h-9.6" ] ],
    [ "sw4:3", "mega:5V.2", "red", [ "v0", "h9.8", "v-57.6", "h134.4" ] ],
    [ "sw5:1", "mega:GND.5", "green", [ "v-67.2", "h144", "v364.8" ] ]
  ],
  "dependencies": {}
}

Sorry i read that wrong, What I posted earlier, was my complete sketch of what i have so far.

As has been pointed out, there are problems with it which I thought that maybe you had corrected

If that sketch does anything usefull for you, it is pure luck. Sorry, but the sketch simply makes no sense.
I suggest to start from zero. Take small steps and test test test.
As to your original question: yes you can multiply arrays and use the result. But atvthis moment that seems far out of your league.

#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27
#define LCD_COLUMNS 16
#define LCD_LINES   2
#define BUTTON_PIN  7 //Trigger
#define BUTTON_PIN1 8 //Trigger
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
char *options[]={"Pie","Apple","Fish","Pizza"};
long option1;


void setup() {
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(31, INPUT); //Ignore for now
  pinMode(34, OUTPUT);//LED #1 (This is my backlight it will stay on, while no trigger has been set)
  pinMode(37, OUTPUT);//LED #2
  pinMode(38, OUTPUT);//LED #3
  lcd.init();
  lcd.backlight();
  randomSeed(4);
 Serial.begin(9600);
 Serial.print(options[option1]);
}
void loop() {
  int state=0;
 state=digitalRead(BUTTON_PIN),(BUTTON_PIN1);
 if(state==HIGH){

 option1 = random(sizeof(options)/sizeof(char*));
 lcd.clear();
 delay(800);
 lcd.print("PROCESSING....");
 lcd.blink();
 delay(4000);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("You Win:");
 lcd.setCursor(0,1);
 lcd.print(options[option1]);
 delay (4000);
 lcd.clear();
 }

 else{
 lcd.setCursor(0,0);
 lcd.print("Ready...");
 lcd.blink();
 delay(1000);
 }

  if (strcmp(options[option1], "Pie") == 0)
    {digitalWrite(37, HIGH);
    delay(5000);}
      {digitalWrite(34, HIGH);
      digitalWrite(37, LOW);
      digitalWrite(38, LOW);}
if (strcmp(options[option1], "Apple") == 0)
    {digitalWrite(37, HIGH);
    delay(15000);}
      {digitalWrite(34, HIGH);
      digitalWrite(37, LOW);
      digitalWrite(38, LOW);}
if (strcmp(options[option1], "Fish") == 0)
    {digitalWrite(38, HIGH);
    delay(5000);}
      {digitalWrite(34, HIGH);
      digitalWrite(37, LOW);
      digitalWrite(38, LOW);}
if (strcmp(options[option1], "Pizza") == 0)
    {digitalWrite(38, HIGH);
    delay(15000);}
      {digitalWrite(34, HIGH);
      digitalWrite(37, LOW);
      digitalWrite(38, LOW);}
  
}

I totally get that, and yeah part of wants to bail. But i invested too much time to just give up. You wont believe me but I literally "Take small steps and test test test."

Any way i could get an example of multiplying an array and using the result?
i can dissect it and play around with it.

Fix your code first. Believe me or not: it is a mess...
In ide press ctrl-t that will line up your code.
Do you really want to run the code after strcmp if the string is not equal to pizza???
It is not really a good idea to use strcmp for this. It is much easier to use an enum...
...and a switch case....

Thank you for your time

Multiplication of an array would look like:

const int arraySize = 4;
int factor = 3;
int myArray[] = {1, 2, 3, 4};
int myResultArray[arraySize];
for (int i=0; i<arraySize; i++) {
  myResultArray[i] = factor * myArray[i];
}
for (int i=0; i<arraySize; i++) {
  Serial.println(myResultArray[i]);
}

Of course it would be better to put this in a function...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.