Pro trinket EEPROM read and write - arduino

(I am using Arduino IDE)
The Thing I am trying to achieve is it saving the "progress" even when the microcontroller turns off. I have tried for a while tweaking the code but I have yet to get it to actually save/read values from EEPROM properly, so here's the part of my code(Ignore the rest for running the display+i have removed all of the irrelevant code):
#include <EEPROM.h>
int hunger = 10000;
int happiness = 10000;
int y = 0;
int z = 0;
int sensorPin = A0;
int sensorPin2 = A1;
void setup() {
Serial.begin(9600);
EEPROM.write(5, 10000);
EEPROM.write(5, 10000);
hunger = EEPROM.read(8)- '0';
happiness = EEPROM.read(5)- '0';
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.display();
Serial.begin(9600);
pinMode(sensorPin,INPUT);
}
void loop() {
int value = happiness;
int value2 = hunger;
EEPROM.write(5, happiness);
EEPROM.write(8, hunger);
hunger -= 1;
happiness -= 1;
if(analogRead(sensorPin)>1020){
testhappy();
happiness+=2500;
}
else{
testsad();
}
while(analogRead(sensorPin2)>1020){
testeat();
hunger+=5000;
}
}
void testhappy(void){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(30, 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
delay(1);
display.display();
delay(1);
for(y=0;y<2;y++){
for(z=0;z<5;z++){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
for(z=5;z!=0;z-=1){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
for(z=0;z> -5;z-=1){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
for(z=0-5;z!=0;z+=1){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
}
display.stopscroll();
}
void testsad(void){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(30, 32, Mild32_glcd_bmp, 32, 32, 1);
display.display();
delay(1);
}
void testeat(void){
delay(200);
for(y=0;y<6;y++){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(30, 32, Eat32_glcd_bmp, 32, 32, 1);
display.drawBitmap(60, 38, Cookie18_glcd_bmp, 8, 8, 1);
display.display();
delay(200);
display.clearDisplay();
display.setCursor(0,0);
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(60, 38, Cookie28_glcd_bmp, 8, 8, 1);
display.drawBitmap(30, 32, Mild32_glcd_bmp, 32, 32, 1);
display.display();
delay(200);
}
}
void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
uint8_t icons[NUMFLAKES][3];
// initialize
for (uint8_t f=0; f< NUMFLAKES; f++) {
icons[f][XPOS] = random(display.width());
icons[f][YPOS] = 0;
icons[f][DELTAY] = random(5) + 1;
Serial.print("x: ");
Serial.print(icons[f][XPOS], DEC);
Serial.print(" y: ");
Serial.print(icons[f][YPOS], DEC);
Serial.print(" dy: ");
Serial.print(icons[f][DELTAY], DEC);
}
while (1) {
// draw each icon
for (uint8_t f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
}
display.display();
delay(200);
// then erase it + move it
for (uint8_t f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK);
// move it
icons[f][YPOS] += icons[f][DELTAY];
// if its gone, reinit
if (icons[f][YPOS] > display.height()) {
icons[f][XPOS] = random(display.width());
icons[f][YPOS] = 0;
icons[f][DELTAY] = random(5) + 1;
}
}
}
}
(I am getting no errors when compiling the code)

Look here at the page for EEPROM.write here. https://www.arduino.cc/en/Reference/EEPROMWrite
There it says the arguments are defined as:
EEPROM.write(address, value)
Parameters address: the location to write to, starting from 0 (int)
value: the value to write, from 0 to 255 (byte)
Now look at the number you're trying to write into EEPROM:
EEPROM.write(5, 10000);
Hopefully now you can see what the problem is. Your second argument is far too large. You should break it into two bytes or use a different method like EEPROM.put (look that up) which doesn't suffer this limitation.

Related

Arduino Nano IoT cannot read onboard IMU and analogRead() in the same loop

#include <Arduino_LSM6DS3.h>
#include <avr/dtostrf.h>
void setup()
{
Serial.begin(9600);
while (!Serial); // wait for Serial
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.println ("Starting");
}
void loop()
{
char msg[100]="";
char msg2[100]="";
char buffer[8];
char buffer2[8];
readIMU(msg,buffer);
readFlex(msg2, buffer2);
Serial.println(msg);
Serial.println(msg2);
delay(1000);
}
void readIMU(char message[100], char buffer[8])
{
float x,y,z,x1,y1,z1;
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(x, y, z);
dtostrf(x, 4, 2, buffer);
strcat(message, buffer);
strcat(message, ", ");
dtostrf(y, 4, 2, buffer);
strcat(message, buffer);
strcat(message, ", ");
dtostrf(z, 4, 2, buffer);
strcat(message, buffer);
strcat(message, ", ");
}
if (IMU.accelerationAvailable()){
IMU.readAcceleration(x1, y1, z1);
dtostrf(x1, 4, 2, buffer);
strcat(message, buffer);
strcat(message, ", ");
dtostrf(y1, 4, 2, buffer);
strcat(message, buffer);
strcat(message, ", ");
dtostrf(z1, 4, 2, buffer);
strcat(message, buffer);
strcat(message, ", ");
}
}
void readFlex(char message[100], char buffer[8])
{
int value = analogRead(1);
ltoa(value,buffer, 10);
strcat(message, buffer);
strcat(message, ", ");
delay(10);
value = analogRead(2);
ltoa(value,buffer, 10);
strcat(message, buffer);
delay(10);
strcat(message, ", ");
delay(10);
value = analogRead(3);
ltoa(value,buffer, 10);
strcat(message, buffer);
strcat(message, ", ");
delay(10);
value =0;
value = analogRead(4);
ltoa(value,buffer, 10);
strcat(message, buffer);
strcat(message, ", ");
delay(10);
value =0;
value = analogRead(5);
ltoa(value,buffer, 10);
strcat(message, buffer);
strcat(message, "\0");
delay(10);
}
If I comment out the either the readIMU() or readFlex(), the other works fine. But if I uncomment both function, the readIMU() seems to not read value anymore and keep print the same value every loop like below with the exception of the first loop() . The only exception that it work is that if I only read A1 and A2, issue occur if it reads A3-A5 as well. I only put in the post the IMU reading. Am I missing anything here?

how to fix error "id return 1 exit status return code is not 0" in C++?

//Program by: Alim Mulyadi
//tgl: 21/02/2021
//Program: Countdown Timer Sederha1. na
[enter image description here](https://i.stack.imgur.com/58w5W.jpg)
#include <LiquidCrystal_I2C.h>
#include "Countimer.h"
Countimer tdown;
LiquidCrystal_I2C lcd(0x3F, 16, 2);
#define pb_set A0 //Set
#define pb_up A1 //Up dan Start
#define pb_down A2 //Down, Pause, dan Reset
int relay = 4;
int buzzer = 9;
int set_detik = 0;
int set_menit = 0;
int set_jam = 0;
int last_set_detik = 0;
int last_set_menit = 0;
int last_set_jam = 0;
int set = 0;
bool kondisi_set = 0;
bool kondisi_relay = 0;
bool kondisi_reset = 0;
unsigned long lastmillis;
void setup() {
Serial.begin (9600);
pinMode(pb_set, INPUT_PULLUP);
pinMode(pb_up, INPUT_PULLUP);
pinMode(pb_down, INPUT_PULLUP);
pinMode(relay, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd.begin();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown Timer");
lcd.setCursor(0, 1);
lcd.print("By: Alim Mulyadi");
tdown.setInterval(print_time, 1000);
delay(2000);
lcd.clear();
}
void print_time() {
set_detik = set_detik - 1;
if (set_detik < 0) {
set_detik = 59;
set_menit = set_menit - 1;
}
if (set_menit < 0) {
set_menit = 59;
set_jam = set_jam - 1;
}
}
void loop() {
tdown.run();
if (digitalRead (pb_set) == 0) {
if (kondisi_set == 0 && kondisi_relay == 0) {
kondisi_set = 1;
set = set + 1;
if (set > 3) {
set = 0;
}
delay(100);
}
} else {
kondisi_set = 0;
}
if (digitalRead (pb_up) == 0) {
if (set == 0) {
tdown.start();
kondisi_relay = 1;
}
if (set == 1) {
set_detik++;
last_set_detik = set_detik;
}
if (set == 2) {
set_menit++;
last_set_menit = set_menit;
}
if (set == 3) {
set_jam++;
last_set_jam = set_jam;
}
if (set_detik > 59) {
set_detik = 0;
}
if (set_menit > 59) {
set_menit = 0;
}
if (set_jam > 99) {
set_jam = 0;
}
delay(200);
}
if (digitalRead (pb_down) == 0) {
//delay(500);
lastmillis = millis();
kondisi_reset = 0;
while (digitalRead (pb_down) == 0 && set == 0) {
if (millis() - lastmillis > 500) {
kondisi_reset = 1;
kondisi_relay = 0;
tdown.stop();
set_detik = last_set_detik;
set_menit = last_set_menit;
set_jam = last_set_jam;
lcd.setCursor(4, 1);
if (set_jam <= 9) {
lcd.print("0");
}
lcd.print(set_jam);
lcd.print(":");
if (set_menit <= 9) {
lcd.print("0");
}
lcd.print(set_menit);
lcd.print(":");
if (set_detik <= 9) {
lcd.print("0");
}
lcd.print(set_detik);
lcd.print(" ");
delay(100);
}
}
if (kondisi_reset == 0) {
if (set == 0) {
tdown.stop();
kondisi_relay = 0;
}
if (set == 1) {
set_detik--;
last_set_detik = set_detik;
}
if (set == 2) {
set_menit--;
last_set_menit = set_menit;
}
if (set == 3) {
set_jam--;
last_set_jam = set_jam;
}
if (set_detik < 0) {
set_detik = 59;
}
if (set_menit < 0) {
set_menit = 59;
}
if (set_jam < 0) {
set_jam = 99;
}
delay(200);
}
}
lcd.setCursor(0, 0);
if (set == 0) {
lcd.print(" Timer ");
}
if (set == 1) {
lcd.print(" Set Timer SS ");
}
if (set == 2) {
lcd.print(" Set Timer MM ");
}
if (set == 3) {
lcd.print(" Set Timer HH ");
}
lcd.setCursor(4, 1);
if (set_jam <= 9) {
lcd.print("0");
}
lcd.print(set_jam);
lcd.print(":");
if (set_menit <= 9) {
lcd.print("0");
}
lcd.print(set_menit);
lcd.print(":");
if (set_detik <= 9) {
lcd.print("0");
}
lcd.print(set_detik);
lcd.print(" ");
if (set_detik == 0 && set_menit == 0 && set_jam == 0 && kondisi_relay == 1) {
kondisi_relay = 0;
tdown.stop();
digitalWrite(relay, LOW);
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
}
if (kondisi_relay == 1) {
digitalWrite(relay, HIGH);
}
else {
digitalWrite(relay, LOW);
}
delay(1);
}
I hope to find the answer

Arduino multiple pin ADC read

Trying to read the value's from two analog sensors.
Can you use the for loop to read analog pins?
int i;
for (i = 0; i < 2; i = i + 1) {
x[I]=(analogRead(A[i]);
Working Code:
int AnalogpIn[2];
int MapValue[2];
void setup() {
Serial.begin(9600);
}
void loop() {
delay(100);
AnalogpIn[0] = analogRead(A0);
MapValue[0] = map(AnalogpIn[0], 0, 1023, 0, 255);
delay(100);
AnalogpIn[1] = analogRead(A1);
MapValue[1] = map(AnalogpIn[1], 0, 1023, 0, 255);
int i;
for (i = 0; i < 2; i = i + 1) {
Serial.print(AnalogpIn[i]);
Serial.print("-");
Serial.print(MapValue[i]);
Serial.println("");
delay (100);
}
}
Try this out,
I think this is what you are looking for. Have made few changes.
void loop(){
int i=0;
for (int i = 0; i < 2; i++) {
AnalogpIn[i] = analogRead(i);
MapValue[i] = map(AnalogpIn[i], 0, 1023, 0, 255);
Serial.print(AnalogpIn[i]);
Serial.print("-");
Serial.print(MapValue[i]);
Serial.println("");
}
delay (100);
}

Flex sensor aren't reacted. arduino

I'm trying controlling the 5 servo snd Flex sensor. So, I connect the Arduino with Bled Board(Flex sensor and 5 servo).
However, though I bended these Flex sensors, they are not reacted.
that code is below.
// letsarduino.com
// [Project 15] - 5 Flex/Bend Sensor + 5 Servo Motor + Arduino
#include <Servo.h>
Servo servoMotor1;
Servo servoMotor2;
Servo servoMotor3;
Servo servoMotor4;
Servo servoMotor5;
int servoPin1 = 2;
int servoPin2 = 3;
int servoPin3 = 4;
int servoPin4 = 5;
int servoPin5 = 6;
void setup() {
servoMotor1.attach(servoPin1);
servoMotor2.attach(servoPin2);
servoMotor3.attach(servoPin3);
servoMotor4.attach(servoPin4);
servoMotor5.attach(servoPin5);
}
void loop()
{
int analogValue = analogRead(A0);
int servoAngle = map(analogValue, 550, 640, 0, 179);
servoMotor1.write(servoAngle);
int analogValue2 = analogRead(A1);
int servoAngle2 = map(analogValue2, 550, 640, 0, 179);
servoMotor2.write(servoAngle2);
int analogValue3 = analogRead(A2);
int servoAngle3 = map(analogValue3, 550, 640, 0, 179);
servoMotor3.write(servoAngle3);
int analogValue4 = analogRead(A3);
int servoAngle4 = map(analogValue4, 550, 640, 0, 179);
servoMotor4.write(servoAngle4);
int analogValue5 = analogRead(A4);
int servoAngle5 = map(analogValue5, 550, 640, 0, 179);
servoMotor5.write(servoAngle5);
}
Do you know why these flex sensors and servo motors are not reacted?
I think Wiring between bred board and arduino, flex sensors and servo motors are not mistaken.

Error with Arduino/Processing serial communication

I am relatively new to both Arduino and Processing and I have been working on a code that utilizes serial communication between the two. My Arduino code is reading and printing values from a piezo sensor and then sending the values to Processing, which sketches certain shapes based on the values. The code has worked previously, but for some reason it is no longer working. Everything compiles, but when I run the Processing code the sketch window is empty and remains empty. A few times the "error, disabling serialEvent()" showed up, but I just unplugged my Arduino board, closed out the programs, and restarted everything. The error no longer shows up, but my Processing sketch is still not displaying on the screen. Can someone please let me know what is wrong with my code? I really appreciate the help.
Arduino Code:
int ledPin = 13;
int knockSensor = A0;
byte val = 0;
int statePin = LOW;
int THRESHOLD = 5;
int sensorReading = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorReading = analogRead(knockSensor);
if(sensorReading > 0)
{
Serial.println(sensorReading, DEC);
}
if (sensorReading != 0)
Serial.println(sensorReading);
delay(100);
}
Processing Code:
import processing.serial.*;
Serial port;
int centerX = 550;
int centerY = 400;
float val;
float ellipseX;
float ellipseY;
float ellipseW;
float ellipseH;
float ellipseXX;
float ellipseYY;
float ellipseWW;
float ellipseHH;
float lineX;
float lineY;
float lineXX;
float lineYY;
void setup(){
background(255);
size(1100,800);
frameRate(10);
smooth();
String portname = "/dev/tty.usbmodem1411";
//String portname = Serial.list()[0];
port = new Serial(this, portname, 9600);
println(Serial.list());
//port.bufferUntil('\n');
}
void drawEllipse(float val)
{
if(val > 0 && val < 50)
{
ellipseX = random(540,560);
ellipseY = random(390,410);
ellipseW = val + 10;
ellipseH = val + 10;
stroke(0);
fill(random(255), random(200,255));
}
}
void drawLines(float val)
{
if(val > 50 && val < 70)
{
lineX = random(500, 600);
lineY = random(360, 440);
lineXX = random(500, 600);
lineYY = random(360, 440);
stroke(0);
}
}
void drawEllipse2(float val)
{
if(val > 70 && val < 120)
{
ellipseXX = random(460, 640);
ellipseYY = random(330, 470);
ellipseWW = val + random(20);
ellipseHH = val + 10;
stroke(0);
fill(random(50, 100), random(50, 100), random(50, 100), random(220, 255));
}
}
void serialEvent(Serial port)
{
String inString = port.readStringUntil('\n');
if (inString != null)
{
val = Float.parseFloat(inString);
}
drawEllipse(val);
drawLines(val);
drawEllipse2(val);
println(val);
}
Maybe using Serial.write() will be better. So the code will look like this.
Arduino Code:
int ledPin = 13;
int knockSensor = A0;
byte val = 0;
int statePin = LOW;
int THRESHOLD = 5;
int sensorReading = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorReading = analogRead(knockSensor);
if(sensorReading > 0)
{
Serial.println(sensorReading, DEC);
}
if (sensorReading != 0)
Serial.write(map(sensorReading, 0, 1023, 0, 255));
delay(100);
}
Processing Code:
import processing.serial.*;
Serial port;
int centerX = 550;
int centerY = 400;
float val;
float ellipseX;
float ellipseY;
float ellipseW;
float ellipseH;
float ellipseXX;
float ellipseYY;
float ellipseWW;
float ellipseHH;
float lineX;
float lineY;
float lineXX;
float lineYY;
void setup(){
background(255);
size(1100,800);
frameRate(10);
smooth();
String portname = "/dev/tty.usbmodem1411";
//String portname = Serial.list()[0];
port = new Serial(this, portname, 9600);
println(Serial.list());
//port.bufferUntil('\n');
}
void drawEllipse(float val)
{
if(val > 0 && val < 50)
{
ellipseX = random(540,560);
ellipseY = random(390,410);
ellipseW = val + 10;
ellipseH = val + 10;
stroke(0);
fill(random(255), random(200,255));
}
}
void drawLines(float val)
{
if(val > 50 && val < 70)
{
lineX = random(500, 600);
lineY = random(360, 440);
lineXX = random(500, 600);
lineYY = random(360, 440);
stroke(0);
}
}
void drawEllipse2(float val)
{
if(val > 70 && val < 120)
{
ellipseXX = random(460, 640);
ellipseYY = random(330, 470);
ellipseWW = val + random(20);
ellipseHH = val + 10;
stroke(0);
fill(random(50, 100), random(50, 100), random(50, 100), random(220, 255));
}
}
void serialEvent(Serial port)
{
if (0 < port.available()) {
val = map(port.read(), 0, 255, 0, 1023);
}
if (val > 0) {
drawEllipse(val);
drawLines(val);
drawEllipse2(val);
println(val);
}
}

Resources