Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This code does not compile as presented and has some minor logic errors. Fix the code such that it reads from the four switches, the

This code does not compile as presented and has some minor logic errors. Fix the code such that it reads from the four switches, the temperature sensor, the potentiometer, and the light sensor 250 times with a 20-millisecond delay between readings. The results should be displayed in the serial console window and should look something like this: time: 0 ms potentiometer: 0.50 temperature: 0.14 light: 0.90 switch0: 1 switch1: 1 switch2: 1 switch3: 1 --------------------------- time: 20 ms potentiometer: 0.65 temperature: 0.20 light: 0.89 switch0: 1 switch1: 1 switch2: 1 switch3: 1 --------------------------- (continuing for all 250 measurements) This is what I have so far, as you can see it is in C code for the Arduino system. Thanks ///////////////////////////////////////////////////////////////////////////// // Symbolic Constants #define LED0 = 11 #define LED1 = 9 #define LED2 = 6 #define LED3 = 3 #define SPKR = 5 #define SRVO = 10 #define RED = 11 #define GREEN = 9 #define BLUE = 6 #define SW0 = 12 #define SW1 = 8 #define SW2 = 7 #define SW3 = 4 #define TEMP = A0 #define POT = A1 #define LIGHT = A2 #define ANAINMAX // enter the max value for analog reads here #define ANAOUTMAX // enter the max value for analog writes here #define BAUD = 9600 #define SAMPLES = 100 #define MEASUREDELAY ///////////////////////////////////////////////////////////////////////////// // setup void setup() { pinMode( LED0, OUTPUT ); pinMode( LED1, OUTPUT ); pinMode( LED2, OUTPUT ); pinMode( LED3, OUTPUT ); pinMode( SPKR, OUTPUT ); pinMode( SRVO, OUTPUT ); pinMode( SW0, INPUT ); pinMode( SW1, INPUT ); pinMode( SW2, INPUT ); pinMode( SW3, INPUT ); pinMode( TEMP, INPUT ); pinMode( POT, INPUT ); pinMode( LIGHT, INPUT ); Serial.begin( BAUD ); } ///////////////////////////////////////////////////////////////////////////// // loop void loop() { int switch0 = 0; int switch1 = 0; int switch2 = 0; int switch3 = 0; float temperature = 0; float pot = 0; float light = 0; int counter = 0; wait_for_start(); for( counter = 0; counter < SAMPLES; ++counter ) { read_switches( switch0, switch1, switch2, switch3 ); pot = read_pot( POT ); temperature = read_temp( TEMP ); light = read_light( LIGHT ); set_LEDs( switch0, switch1, switch2, switch3, pot, temperature, light ); print_stats( switch0, switch1, switch2, switch3, pot, temperature, light, MEASUREDELAY ); delay(MEASUREDELAY); } mark_end(); } ///////////////////////////////////////////////////////////////////////////// // wait_for_start // Waits for a keypress in the Serial window void wait_for_start() { Serial.print("Press and release switch 0 "); while( HIGH == digitalRead(SW0) ) {} Serial.print("Release switch 0 "); while( LOW == digitalRead(SW0) ) {} } ///////////////////////////////////////////////////////////////////////////// // read_switches // Reads the state of the four switches and updates passed in variables void read_switches( int *switch0, int *switch1, int *switch2, int *switch3 ) { switch0 = digitalRead( SW0 ); switch1 = digitalRead( SW1 ); switch2 = digitalRead( SW2 ); switch3 = digitalRead( SW3 ); } ///////////////////////////////////////////////////////////////////////////// // read_pot // Reads the potentiometer and returns its value as a percentage of the range float read_pot( int port ) { int pot = analogRead( port ); return pot / ANAINMAX; } ///////////////////////////////////////////////////////////////////////////// // read_temp // Reads the temp sensor and returns its value as a percentage of the range float read_temp( int port ) { int temp = analogRead( port ); return temp / ANAINMAX; } ///////////////////////////////////////////////////////////////////////////// // read_light // Reads the light sensor and returns its value as a percentage of the range float read_light( int port ) { int light = analogRead( port ); return light / ANAINMAX; } ///////////////////////////////////////////////////////////////////////////// // set_LEDs void set_LEDs( int swtich0, int switch1, int switch2, int switch3, float pot, float temperature, float light ) { if( LOW = switch0 ) { analogWrite( LED0, pot * ANAOUTMAX ); } else { digitalWrite( LED0, LOW ); } if( LOW = switch1 ) { analogWrite( LED1, temperature * ANAOUTMAX ); } else { digitalWrite( LED1, LOW ); } if( LOW = switch2 ) { analogWrite( LED2, light * ANAOUTMAX ); } else { digitalWrite( LED2, LOW ); } analogWrite( LED3, ((pot + temperature + light) / 3.0) * ANAOUTMAX ); } ///////////////////////////////////////////////////////////////////////////// // print_stats // prints settings to console window void print_stats( int switch0, int switch1, int switch2, int switch3, float pot, float temperature, float light, int MEASUREDELAY ) { static int counter = 0; Serial.print("time: "); Serial.print(counter); Serial.print(" ms "); Serial.print("switch0: "); Serial.print(swtich); Serial.print(" "); Serial.print("switch1: "); Serial.print(swtich); Serial.print(" "); Serial.print("switch2: "); Serial.print(swtich); Serial.print(" "); Serial.print("switch3: "); Serial.print(swtich); Serial.print(" "); Serial.print("potentiometer: "); Serial.print(pot); Serial.print(" "); Serial.print("temperature: "); Serial.print(temperature); Serial.print(" "); Serial.print("light: "); Serial.print(light); Serial.print(" "); Serial.print("--------------------------- "); // a command is missing here. Roughly how often does this run? How does // that relate to the time? } ///////////////////////////////////////////////////////////////////////////// // mark_end // marks the end of a run int mark_end() { Serial.print("ENDENDENDENDENDENDENDENDENDENDENDENDENDEND "); } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

Give push-down automata for the languages

Answered: 1 week ago

Question

What could we do to develop our working relationship?

Answered: 1 week ago